当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP.NET > ASP.NET 维持浏览状态

ASP.NET
ASP.NET 2.0 - Navigation 导航
C#中new和override区别
ASP.NET中 Bin,App_Browser,App_code,App_Data,App_Theme 等文件

ASP.NET 维持浏览状态


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 959 ::
收藏到网摘: n/a

You may save a lot of coding by maintaining the ViewState of the objects in your Web Form.
你可以在你的WEB表单中通过维持浏览状态来省掉你许多代码。


Maintaining the ViewState
维持浏览状态

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.
当在经典ASP中提交表单,所有表单单里的值都会被清空。假设你已经提交了有很多信息的表单并且服务器返回了错误信息,你必须重新纠正表单里的信息。你点击后退按钮,发生的事就是所有的信息都被清空了,而你将必须从新开始!站点无法维持你的浏览状态。

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:
当在ASP.NET中提交表单,表单里所有的信息将会重新显示到浏览器上。怎么来的?这就是因为ASP.NET可以维持你的浏览状态。这个浏览状态就象征当你提交到服务器时的页面状态。状态信息会通过<form runat="server">控件保存在一个隐藏的区域中。源代码看起来就刑这样:

<form name="_ctl0" method="post" action="page.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE"
value="dDwtNTI0ODU5MDE1Ozs+ZBCF2ryjMpeVgUrY2eTj79HNl4Q=" />
.....some code
.....一些代码
</form>

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.
默认情况下ASP.NET为维持WEB表单的浏览状态。如果你想维持状态,在aspx文件的页顶加上指示 <%@ Page EnableViewState="false" %> 或是在任何控件上加上属性EnableViewState="false"

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:
看看下面的.aspx文件。它将演示“老”方法。当你按了提交按钮,表单里的信息将会消失:

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

Example

Here is the new ASP .NET way. When you click on the submit button, the form value will NOT disappear:
这里是用ASP.NET的新方法。当你按了提交按钮,表单里的信息不会消失:

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

Example (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)
(可以查看右框架的源代码来查看ASP.NET往隐藏区域添加了浏览状态信息)

评论 (1) 1 All

登陆 | 还没注册?