Code behind variable in aspx pages

on Tuesday, May 5, 2009

For asp.net Server Controls if you want to access code behind variable in aspx pages. There are 3 things to remember:
1. Scope of the variable has to be protected or Public
2. The variable can be accessed only using DataBinding Syntax like below.
<asp:TextBox ID="TextBox1" runat="server" Text='<%# DateTime.Now.ToString()%>'></asp:TextBox>
3.You have to Call Page.DataBind() or TextBox1.DataBind() method in the Page_Load event or any other event before Page_Render event's base.Render(writer) method call.

For HTML controls you could just use the traditional syntax like below.
<input type='text' value='<%=DateTime.Now.ToString()%>' />

0 comments: