asp.net后台前台代码相互绑定大总结

系统 1814 0

一.把后台代码写到前台去


1 <wbr></wbr> 对于普通的 html 标签和普通服务器控件绑定后台变量(变量一定要 public

.cs

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>// 加载方式<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><strong style="font-weight:bold">public</strong><span><wbr></wbr></span>string loadtype;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>protected void Page_Load(object sender, EventArgs e)<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>{<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>// 检测是否首次加载<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>if (Request.QueryString["loadType"] != null)<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>{<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>loadtype = Request.QueryString["loadType"].ToString();<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>}</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr></wbr></wbr>

.aspx

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>&lt;div style='display:&lt;%=loadtype=="1"?"none":"inline"%&gt;'&gt;............&lt;/div&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr></wbr></wbr>

先通过 <%= loadtype%>标签获取到loadtype,再对loadtype进行判断, 这个时候<%=loadtype%>可能不变黄,没有关系。

<wbr><wbr></wbr></wbr>

<wbr><wbr></wbr></wbr>

2. Gridview repeater 数据绑定控件里绑定后台变量

<wbr><wbr></wbr></wbr>

我们知道,不是那个角色都可以设置监控点的。那么我需要在绑定的时候,判断一下,这个人的usertpye是多少。那么怎么办判断呢?看代码:

<wbr><wbr><wbr></wbr></wbr></wbr>

<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<a href="EditUser.aspx?userId=<%# eval_r("ID") %>">修改</a>

<a href="EditPWD.aspx?userId=<%# eval_r("ID") %>">重设密码</a>
<%#Convert.ToUInt32(eval_r("UserType")) == 7 ?

"<a href=../Point.aspx?userid=" + eval_r("ID") + ">分配监控点</a>" : ""%>

<wbr><wbr><wbr>&lt;/ItemTemplate&gt;<br><wbr><wbr><wbr>&lt;/asp:TemplateField&gt;</wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr></wbr></wbr></wbr>

简单吧!其实其他的控件如repeater,datalist等等,都可以效法!

<wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr>

我们再看一例:

<asp:LinkButton ID="linkbtnCloseOrOpen" runat="server" CommandName='<%#eval_r("IsClosed") %>' CommandArgument='<%#eval_r("ID") %>'>
<%#bool.Parse(eval_r("IsClosed").ToString())?"打开监控点":"关闭监控点" %>
</asp:LinkButton>

<wbr><wbr></wbr></wbr>

<wbr><wbr></wbr></wbr>

3.引申——绑定后台方法

由1,2的区别,看二者3的区别——也就是<%=%>和<%#%>绑定方法的区别,懂了吧! asp.net前台绑定后台代码大总结

<%=Add()%>

<%#Add()%>

拿第二种举个例子吧

在后台有这样的方法

<wbr></wbr>

<wbr>public Test_Paper GetPaper(int id)</wbr>

{

<wbr><wbr><wbr><wbr>return BLL.Test_PaperBLL.ReadTestPaperByID(id);</wbr></wbr></wbr></wbr>

}

在前台这样用
<%#GetPaper(Convert.ToInt32(eval_r("PaperNum"))).Name %>)

需要注意的是 eval_r("PaperNum")获取的是object类型,注意类型转化。

再看例子
<%#GetChoose(eval_r("QId")).OptionA == null ? "" : "<input type='radio' name='choose"+eval_r("QId")+"'>A." + GetChoose(eval_r("QId")).OptionA + "</input>"%>


4.<%%>干吗用啊?

这样用:

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;%<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>int countValue = id;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>%&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<div<wbr><wbr><span><wbr></wbr></span>style='display:&lt;%=countValue==1?"none":"inline"%&gt;'&gt;&lt;/div&gt;</wbr></wbr>

<wbr><wbr></wbr></wbr>

①<%%>中可以获取后台中public的字段,像例子中的id

②可以声明变量

③变量可以再前台使用

④可以再<%%>对后台的数据进行一些逻辑操作,当然,这种逻辑操作可以在后台完成,再在前台获取后台方法,如本文的第三个大标题一样!

<wbr><wbr></wbr></wbr>

<wbr><wbr></wbr></wbr>

再看一例

<%if (user.CompanyId==1)
{<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr>%&gt;<br></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;tr&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;td align="left" class="box"<wbr><wbr><span><wbr></wbr></span>onclick="hiddenTree('table13'住建局安办&lt;/td&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/tr&gt;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;tr&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;td align="left" class="text3"&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;table id="table13" style="display:none;" width="100%" border="0" cellspacing="0"</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>cellpadding="0"&gt;<wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>&lt;tr&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>&lt;td align="left" class="text3"&gt;&lt;a href="/Manage/CheckPoints.aspx"</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>target="mainFrame"&gt;住建局监控&lt;/a&gt;&lt;/td&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>&lt;/tr&gt;<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span><wbr><wbr><wbr><wbr><wbr><wbr><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/table&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/td&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/tr&gt;<br><strong style="font-weight:bold">&lt;%} %&gt;</strong><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr></wbr>

<wbr></wbr>

<wbr></wbr>

二把前台代码写到后台去


我们不仅可以在前台写代码,也可以在后台写前台的代码

<wbr><wbr></wbr></wbr>

比如:

<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>&lt;table width="100%" border="0" cellspacing="0" cellpadding="0"&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><strong style="font-weight:bold"><span style="color:#ed1c24; line-height:21px; word-wrap:normal; word-break:normal">&lt;%=str2.ToString()%&gt;<br></span></strong><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;tr&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;td width="37%" height="30"&gt;&lt;/td&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;td width="31%" height="30"&gt;&lt;/td&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;td width="32%" height="30"&gt;&amp;nbsp;&lt;/td&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>&lt;/tr&gt;<br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><span><wbr></wbr></span>&lt;/table&gt;</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

<wbr><wbr></wbr></wbr>

那么在后台的话:

<wbr><wbr></wbr></wbr>

j += 1;
str2.Append("<tr>");
str2.Append("<td id=\"td2" + j + "\" width=\"37%\" height=\"30\" align=\"left\">");
str2.Append(j <wbr>+ GetEvaluation(resultInfo.EvaluationID).Evaluation);<br> str2.Append("&lt;/td&gt;");<br> str2.Append("&lt;td width=\"31%\" height=\"30\"&gt;");</wbr>

str2.Append("<input name=\"resultId2" + j + "\" value=\"" + resultInfo.ID + "\" type=\"hidden\">");
..............................................................................

str2.Append("</tr>");

参考代码:

http://blog.csdn.net/woshixuye/article/details/7218777

asp.net后台前台代码相互绑定大总结


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论