WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

ASP.NET Web Forms - Maintaining the ViewState


You may spare a ton of coding by keeping up the ViewState of the items in your Web Form.


Maintaining the ViewState

At the point when a structure is submitted in exemplary ASP, all structure esteems are cleared. Assume you have presented a structure with a great deal of data and the server returns with a mistake. You should return to the structure and right the data. You click the back catch, and what happens.......ALL structure esteems are CLEARED, and you should start from the very beginning once more! The site did not keep up your ViewState.

At the point when a structure is submitted in ASP .NET, the structure returns in the program window together with all structure esteems. Why? This is since ASP .NET keeps up your ViewState. The ViewState demonstrates the status of the page when submitted to the server. The status is characterized through a concealed field set on each page with a <form runat="server"> control. The source could look something like this:

<form name="_ctl0" method="post" action="page.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE"
value="dDwtNTI0ODU5MDE1Ozs+ZBCF2ryjMpeVgUrY2eTj79HNl4Q="/>

.....some code

</form>

Keeping up the ViewState is the default setting for ASP.NET Web Forms. In the event that you need to NOT keep up the ViewState, incorporate the mandate <%@ Page EnableViewState="false" %> at the highest point of an .aspx page or include the trait EnableViewState="false" to any control.

Look at the accompanying .aspx document. It shows the "old" approach to do it. At the point when you click on the submit catch, the structure esteem will vanish:

Example

<html>
<body>

<form action="demo_classicasp.aspx" method="post">
Your name: <input type="text" name="fname" size="20">
<input type="submit" value="Submit">
</form>
<%
diminish fname
fname=Request.Form("fname")
On the off chance that fname<>"" Then
Response.Write("Hello " & fname & "!")
End If
%>

</body>
</html>

Here is the new ASP .NET way. When you click on the submit catch, the structure esteem won't disappear:

Example

Click see source in the correct casing of the guide to see that ASP .NET has included a concealed field in the structure to keep up the ViewState

<script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Hello " & txt1.Text & "!"
End Sub
</script>

<html>
<body>

<form runat="server">
Your name: <asp:TextBox id="txt1" runat="server"/>
<asp:Button OnClick="submit" Text="Submit" runat="server"/>
<p><asp:Label id="lbl1" runat="server"/></p>
</form>

</body>
</html>