📄 aspnet_viewstate.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ASP.NET The ViewState</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>ASP .NET Maintaining the ViewState</h1>
<a href="aspnet_forms.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_textbox.asp"><img alt="next" border="0" src="../images/btn_next.gif" /></a>
<hr />
<p class="intro">You may save a lot of coding by maintaining the ViewState of
the objects in your Web Form.</p>
<hr />
<h2>Maintaining the ViewState</h2>
<p>
When a form is submitted in classic ASP, all form values are cleared. Suppose you
have submitted a form with a lot of information and the server comes back with an error.
You will have to go back to the form and correct the information. You click the
back button, and what happens.......ALL form values are CLEARED, and you will have to start all over
again! The site did not maintain your ViewState.</p>
<p>
When a form is submitted in ASP .NET, the form
reappears in the browser window together with all form values. How come? This is
because ASP .NET maintains your ViewState.
The ViewState indicates the status of the page when submitted to the server. The
status is defined through a hidden field placed on each page with a <form runat="server"> control. The source could look something like this:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><form name="_ctl0" method="post" action="page.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE"
value="dDwtNTI0ODU5MDE1Ozs+ZBCF2ryjMpeVgUrY2eTj79HNl4Q=" /></pre>
<pre>.....some code</pre>
<pre></form></pre>
</td></tr>
</table>
<p>
Maintaining the ViewState is the default setting for ASP.NET Web Forms. If you
want to NOT
maintain the ViewState, include the directive <%@ Page EnableViewState="false" %> at the top of
an .aspx page or add the attribute EnableViewState="false" to any control.</p>
<p>
Look at the following .aspx file. It demonstrates the "old" way to do it. When
you click on the submit button, the form value will disappear: </p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><html>
<body></pre>
<pre><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
%></pre>
<pre></body>
</html></pre>
</td></tr>
</table>
<p><a target="_blank" href="showasp.asp@filename=demo_classicasp">Example</a></p>
<p>
Here is the new ASP .NET way. When you click on the submit button, the form
value will NOT disappear:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><script runat="server">
Sub submit(sender As Object, e As EventArgs)
lbl1.Text="Hello " & txt1.Text & "!"
End Sub
</script></pre>
<pre><html>
<body></pre>
<pre><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></pre>
<pre></body>
</html></pre>
</td></tr>
</table>
<p><a target="_blank" href="showasp.asp@filename=demo_aspnetviewstate">Example</a>
(Click view source in the right frame of the example to see that ASP .NET has
added a hidden field in the form to maintain the ViewState) </p>
<hr />
<a href="aspnet_forms.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_textbox.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -