在HTML中嵌入服务器端C#代码的方式
1 <%=变量或者函数%>
<wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>例如:<input value="<%=myName%>"/></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 <%#变量或者函数%></span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>例如:<asp:Button ID="butTest" runat="server" Text="<%#myButName%>" /></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 <script runat="server"></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></script></span><br style=""><span style="">这种比较少用,如果要这么写,就直接在后台写代码可以了。</span><br style=""><br style=""><span style="">4 <% c#代码 %></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><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;"/>条</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span><script type="text/javascript"></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=<%=pageBar.PageSize%>;</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span></script></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>
1 <%=变量或者函数%>
<wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>例如:<input value="<%=myName%>"/></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 <%#变量或者函数%></span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span>例如:<asp:Button ID="butTest" runat="server" Text="<%#myButName%>" /></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 <script runat="server"></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></script></span><br style=""><span style="">这种比较少用,如果要这么写,就直接在后台写代码可以了。</span><br style=""><br style=""><span style="">4 <% c#代码 %></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><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;"/>条</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span><script type="text/javascript"></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=<%=pageBar.PageSize%>;</span><br style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""></span><wbr style=""><span style=""><span></span></script></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>