⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aspnet_viewstate.asp@output=print

📁 W3Schools tutorial..web designing
💻 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 &lt;form runat=&quot;server&quot;&gt; control. The source could look something like this:</p>

<table class="ex" border="1" width="100%">
<tr><td>
<pre>&lt;form name=&quot;_ctl0&quot; method=&quot;post&quot; action=&quot;page.aspx&quot; id=&quot;_ctl0&quot;&gt;
&lt;input type=&quot;hidden&quot; name=&quot;__VIEWSTATE&quot;
value=&quot;dDwtNTI0ODU5MDE1Ozs+ZBCF2ryjMpeVgUrY2eTj79HNl4Q=&quot; /&gt;</pre>
<pre>.....some code</pre>
<pre>&lt;/form&gt;</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 &lt;%@ Page EnableViewState=&quot;false&quot; %&gt; at the top of 
an .aspx page or add the attribute EnableViewState=&quot;false&quot; to any control.</p>

<p>
Look at the following .aspx file. It demonstrates the &quot;old&quot; 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>&lt;html&gt;
&lt;body&gt;</pre>
<pre>&lt;form action=&quot;demo_classicasp.aspx&quot; method=&quot;post&quot;&gt;
Your name: &lt;input type=&quot;text&quot; name=&quot;fname&quot; size=&quot;20&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;
&lt;%
dim fname
fname=Request.Form(&quot;fname&quot;)
If fname&lt;&gt;&quot;&quot; Then
Response.Write(&quot;Hello &quot; &amp; fname &amp; &quot;!&quot;)
End If
%&gt;</pre>
<pre>&lt;/body&gt;
&lt;/html&gt;</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>&lt;script runat=&quot;server&quot;&gt;
Sub submit(sender As Object, e As EventArgs)
lbl1.Text=&quot;Hello &quot; &amp; txt1.Text &amp; &quot;!&quot;
End Sub
&lt;/script&gt;</pre>
<pre>&lt;html&gt;
&lt;body&gt;</pre>
<pre>&lt;form runat=&quot;server&quot;&gt;
Your name: &lt;asp:TextBox id=&quot;txt1&quot; runat=&quot;server&quot; /&gt;
&lt;asp:Button OnClick=&quot;submit&quot; Text=&quot;Submit&quot; runat=&quot;server&quot; /&gt;
&lt;p&gt;&lt;asp:Label id=&quot;lbl1&quot; runat=&quot;server&quot; /&gt;&lt;/p&gt;
&lt;/form&gt;</pre>
<pre>&lt;/body&gt;
&lt;/html&gt;</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 + -