开发者

ViewState is not required to preserve control values so what does it do?

I have a very simply web page with ViewState disabled everywhere:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" EnableViewState="false" %>

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

<html xmlns="http://www.w3.org/1999/xhtml">

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

    <asp:TextBox runat="server" EnableViewState="false"></asp:TextBox>

    <asp:DropDownList runat="server"  id="mylist" EnableViewState="false">
    <asp:ListItem>my item 1</asp:ListItem>
    <asp:ListItem>my item 2</asp:ListItem>
    <asp:ListItem>my item 3</asp:ListItem>
    <asp:ListItem>my item 4</asp:ListItem>
    <asp:ListItem>my item 5</asp:ListItem>
    <asp:ListItem>my item 6</asp:ListItem>
    </asp:DropDownList>

    <asp:Button runat="server" Text="click me"/>

    </div>
    </form>
</body>
</html>

Code behind

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      Response.Write("Dropdown list value is " + mylist.SelectedValue);

    }
}

As you can see, no viewstate is enabled but it does preserve controls values, see here for the running example http://ww开发者_如何学Pythonw.yart.com.au/stackoverflow/viewstate/test.aspx

Edit

latr0dectus has somewhat answered my question below. But what's an example where ViewState is required practically? I can't see what you need from the form other than control values.


@Petras: ViewState is not required to preserve control values so what does it do?

Controls that implements IPostBackDataHandler uses LoadPostData() method to assign to some properties.

Read this article : Understanding ASP.NET View State

It is a common misconception among developers that view state is somehow responsible for having TextBoxes, CheckBoxes, DropDownLists, and other Web controls remember their values across postback. This is not the case, as the values are identified via posted back form field values, and assigned in the LoadPostData() method for those controls that implement IPostBackDataHandler.


I'm not really sure what your question is.

View state is used in the page lifecycle. After the page is served it is destroyed on the server. Then the browser posts back it also posts back the viewstate. The server can use this in combination with the posted form values to recreate the previous state of the page and then show the changes.

In some cases even with viewstate disabled certain controls will appear to work as if they have viewstate enabled. This is because some controls have what is called "Control State". It operates almost like viewstate, except it cannot be disabled. This is because some controls would cease to function properly without it.

In the example you posted I think you are observing that the selected value of the dropdown is being posted to the server during postback. Not that it was reconstructed from viewstate.

Im adding this information that I found from the following link: http://aspnetresources.com/articles/ViewState

What's the moral of this story? You don't always need view state enabled to maintain page state. "When do I need it though? What's it for then?" Glad you asked. The prime candidates for participation in view state are those controls that don't post back with the HTTP form and controls added or populated dynamically.

Scan to that part of the document and you should find what you are looking for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜