Subscribe

RSS Feed (xml)

Persisting Data on a Web Form between Postbacks in ASP.NET

By default, all ASP.NET HTML Controls and Web Controls will automatically persist
their values between postbacks.
The following page demonstrates a standard HTML input box, an HtmlInputText
control, and a TextBox control, and how the latter two will retain their values after each
postback without any extra code.
<form id=”StateDemo” method=”post” runat=”server”>
<p>Enter values for each box and press the button.</p>
Input Type=Text box: <input type=”text”><br/>
Input Type=Text box with runat=server: <input type=”text” id=”Input1”
runat=”server” /> <br/>
ASP TextBox Web Control: <asp:TextBox id=”TextBox1” runat=”server” /><br/>
<asp:Button ID=”Button1” Runat=”server” Text=”Postback!” />
</form>
Although in classic ASP, it was necessary to use in-line script to set the values of the
HTML form elements,ASP.NET takes care of this tedious chore for you.This is one of
the coolest and simplest features of ASP.NET. Non-form controls, such as Labels, also
persist their values automatically using ViewState, and you can control this behavior by
using the EnableViewState property of these controls.

No comments:

Archives

Variety in the Web World