在HTML中嵌入服务器端C#代码的方式

在HTML中嵌入服务器端C#代码的方式
1 <%=变量或者函数%>
<wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>例如:&lt;input value="&lt;%=myName%&gt;"/&gt;</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>特点:这种代码只能写在非服务器端代码中,如果写在有runat="server"的控件中,将不起作用。</span><br style=""><span style="">2 &lt;%#变量或者函数%&gt;</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>例如:&lt;asp:Button ID="butTest" runat="server" Text="&lt;%#myButName%&gt;" /&gt;</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>特点:需要调用绑定方法,在后台页面的pageload事件中调用:Page.DataBind();</span><br style=""><br style=""><span style="">3 &lt;script runat="server"&gt;</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>string strTest = "前台页面中输出!";</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>protected void Page_Load(object sender, EventArgs e)</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>{</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>Response.Write(strTest);</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>}</span><br style=""><span style=""></span><wbr style=""><span style=""><span></span>&lt;/script&gt;</span><br style=""><span style="">这种比较少用,如果要这么写,就直接在后台写代码可以了。</span><br style=""><br style=""><span style="">4 &lt;% c#代码 %&gt;</span><br style=""><span style="">这种是最早的ASP时代的代码,现在ASP.NET依然支持,不过一般很少用。</span><br style=""><br style=""><span style="">对于第1种,比如又需要在前台绑定后台变量,又需要在后台应用对应的控件对象,则需要将该控件设置为 runat="server",这时候第一种语法不起作用,又不想用第2中方式进行绑定,则麻烦一点可以用客户端js来实现,在js中使用服务器端的变量,然后更新前台的控件即可。</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>例如:</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>&lt;input id="txtPageSize" runat="server" style="width:28px; border-left:0px; border-right:0px; border-top:0px; border-bottom:1px solid #dddddd; text-align:center;"/&gt;条</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>&lt;script type="text/javascript"&gt;</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>document.getElementByIdx_x("txtPageSize").value=&lt;%=pageBar.PageSize%&gt;;</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>&lt;/script&gt;</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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值