浅谈ViewState

参见《Pro ASP.NET 3.5 in C# 2008

 

Once your page code(.aspx) has finished running(and just before the final HTML is rendered and sent to the client), ASP.NET examines all the properties(.aspx) of all the controls on your page. If any of these properties has been changed from its initial state, ASP.NET makes a note of this information in a name/value collection. Finally, ASP.NET takes all the information it has amassed and then serializes it as a Base64 string. (A Base64 string ensures that there aren’t any special characters that wouldn’t be valid HTML.) The final string is inserted in the <form> section of the page as a new hidden field.

The next time the page is posted back, ASP.NET follows these steps:

1.       ASP.NET re-creates the page and control objects based on its default(as defined in the .aspx file). Thus, the page has the same state that it  had when it was first requested.

2.       Next, ASP.NET deserializes the view state information and updates all the controls. This returns the page to the state it was in before it was sent to the client the last time.

3.       Finally, ASP.NET adjusts the page according to the posted back form data. For example, if the client has entered new text in a text box or made a new selection in a list box, that information will be in the Form collection and ASP.NET will use it to tweak the corresponding controls. After this step, the page reflects the current state as it appears to the user.

4.       Now your event-handling code can get involved. ASP.NET triggers the appropriate events, and your code can react to change the page, move to a new page, or perform a completely different operation.

 

Using view state is a great solution because resources can be freed after each request, thereby allowing for scalability to support hundreds or thousands of requests without bogging the server down. However, it still comes with a price. Because view state is stored in the page, it results in a larger total page size. This affects the client doubly, because the client not only needs to receive a larger page, but the client also needs to send the hidden view state data back to the server with the next postback. Thus, it takes longer both to receive and post the page. For simple pages, this overhead is minimal, but if you configure complex, data-heavy controls such as the GridView, the view state information can grow to a size where it starts to exert a toll. In these cases, you can disable view state for a control by setting its EnableViewState property to false. However, in this case you need to reinitialize the control with each postback.

 

 
 

 

下面来看一段代码,在下面的代码中,我们将演示对一个控件禁用ViewState会是什么样的情况。

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>

<body>

<form id="form1" runat="server">  

<asp:Button ID="Button1" runat="server" EnableViewState="false"

    onclick="Button1_Click" Text="Button" />   

</form>

 </body>

</html>

 

public partial class _Default : System.Web.UI.Page

    {

 

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                this.Button1.Text = "hello";

            }

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

        }

}

 

在以上的页面中,aspx定义Button的初始Text”Button”,但是在initialization code中,我们将ButtonText更改为”hello”.所以我们第一次打开该页面时,看到ButtonText”hello”,如果我们不禁用ViewState,点击Button,它的Text依然为”hello”;如果禁用ViewState,点击Button后,它的Text变为”Button”.具体解释如下所示:

 

1.  不禁用ViewState

a.       First Request

                                                                  i.      Create web page(based on the tags in the .aspx file) – Button.Text = “Button”;

                                                                 ii.      Run your initialization code – Button.Text = “hello”;

                                                               iii.      Button.Text is serialized in the ViewState;

                                                               iv.      Render HTML, so a user sees “hello” on the Button.

b.      Postback Request (Click on the button)

                                                                  i.      Create web page(based on the tags in the .aspx file) – Button.Text = “Button”;

                                                                 ii.      Deserialize and apply the view state – Button.Text = “hello”.

                                                               iii.      Run your initialization code – Button.Text = “hello”;

                                                               iv.      Run your event-handling code – Button.Text = “hello”;

                                                                v.      Serialize viewstate and render HTML.

 

2.       禁用ViewState

a.       First Request

                                                                  i.      Create web page(based on the tags in the .aspx file) – Button.Text = “Button”;

                                                                 ii.      Run your initialization code – Button.Text = “hello”;

                                                               iii.      Button.Text is NOT serialized in the ViewState;

                                                               iv.      Render HTML, so a user sees “hello” on the Button.

b.      Postback Request (Click on the button)

                                                                  i.      Create web page(based on the tags in the .aspx file) – Button.Text = “Button”;

                                                                 ii.      Deserialize and apply the view state – Button.Text = “Button”.

                                                               iii.      Run your initialization code – Button.Text = “Button”;

                                                               iv.      Run your event-handling code – Button.Text = “Button”;

                                                                v.      Serialize viewstate and render HTML.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值