By maintaining the viewstate of objects in the web form, you may save a lot of coding work.
Maintain viewstate
In traditional ASP, all form values are cleared when a form is submitted. Suppose you have submitted a form with a lot of information and the server returns an error. You have to return to the form and correct the information. You click the return button, but what happened ....... All form values are cleared and you will have to start from scratch! The site does not maintain your viewstate.
When submitting a form in ASP. NET, the form and all the form values are reproduced in the browser window. Why? This is because ASP. NET maintains your viewstate. Viewstate indicates the status when the page is submitted to the server. This state is defined by placing an implicit domain in each page with the <form runat = "server"> control. The source file may look like this:
<Form name = "_ ctl0" method = "Post" Action = "page. aspx "id =" _ ctl0 "> <input type =" hidden "name =" _ viewstate "value =" ddwtnti0odu5mde1ozs + zbcf2ryjmpevgury2etj79hnl4q = "/>
... Some code
</Form>
Maintaining viewstate for ASP. NET web forms is the default setting. If you wantNoMaintain viewstate. You can specify <% @ page enableviewstate = "false" %> at the top of the. ASPX page, or add the property: enableviewstate = "false" to any control ".
See the. aspx file below. It demonstrates the "old method" approach. When you click the submit button, the form value will disappear:
< Html >
< Body >
< Form Action = " Demo_classicasp.aspx " Method = " Post " >
Your name: < Input type = " Text " Name = " Fname " Size = " 20 " >
< Input type = " Submit " Value = " Submit " >
</ Form >
<%
Dim fname
Fname = Request. Form ( " Fname " )
If fname <> "" Then
Response. Write ( " Hello " & Fname & " ! " )
End if
%>
</ Body >
</ Html >
This is the new ASP. NET method. When you click the submit buttonNoDisappear:
<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>
(Select View Source file in the browser and you will see that ASP. NET has added an implicit field to the form to maintain viewstate ).