ASP.NET 服务器控件,前台绑定后台代码

来源:互联网 发布:ie11有道网络已断开 编辑:程序博客网 时间:2024/05/16 00:46
1。<asp:TextBox ID="TextBox1" runat="server" Text='<%=strDate %>'></asp:TextBox>
原因是这样的,你这用的是服务器控件(runat="server")就得麻烦点
你先得在后台写个方法,如:
protected string GetNameCookie()
{
  return strDate = DateTime.Now.ToString("yyyyMMdd");
} 然后在你的页面(可以从页面点右键然后选“组件设计器”然后选择页面的事件进行编辑)的prerender事件处理中写一行: 
  this.DataBind(); 
最后前台界面,所有需要这个函数的地方使用绑定语法,例如: 
<asp:TextBox ID="TextBox1" runat="server" Text=" <%# this.GetNameCookie() %>" OnPreRender="TextBox1_PreRender"></asp:TextBox>
2。如果你用的不是服务器控件,如
  <input id="input" type ="text" value=<%=strDate %> />就可以了!