ASP. NET maintain viewstate

Source: Internet
Author: User

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 ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.