Friday, March 4, 2011

EnableViewstate = true/false

We have enableviewstate property for all the server controls in ASP.net. We know that its going to have the member datas and values in viewstate across postbacks

What is the actual example for this?

From stackoverflow
  • I don't entirely understand the question, but this article helped me get a better understanding of ViewState - hopefully it will be useful to you...

  • Please read this. This will answer your doubts about viewstate.

    Beginners guide to viewstate

    ViewState Internals

  • For example, a datagrid control will store all databound data in its viewstate so that it will not reload data from its source (DB, etc.) after postback. However, all the data will go across postbacks in the viewstate, which may affect the size of the page.

  • Check any example of a control. To see the effect make enableviewstate as true/ false... you will see its effect

  • Viewstate's purpose in ASP.NET is indeed to persist state across postbacks, where state is the property values of the controls that make up a Web Form's control hierarchy. But it's necessary to distinguish between the different types of state.

    Anything that you assign declaratively to a control at design-time doesn't need to be persisted in viewstate across postbacks. For example, imagine a Label Web control with the following declarative syntax:

    <asp:Label runat="server" Font-Name="Verdana" Text="Hello, World!"></asp:Label>
    

    When the control hierarchy is built in the instantiation stage, the Label's Text property will be set to "Hello, World!" and its Font property will have its Name property set to Verdana. Since these properties are set on each and every page visit during the instantiation stage, there's no need to persist this information in viewstate.

    What does need to be stored in viewstate is any programmatic changes to the page's state. For example, suppose that in addition to this Label Web control, the page also contained two Button Web controls, a Change Message Button and an Empty Postback button. The Change Message Button has a Click event handler that assigns the Label's Text property to "Goodbye, World!". The Empty Postback Button just causes a postback, but doesn't execute any code. The change to the Label's Text property in the Change Message Button would need to be saved in viewstate.

    Simple control properties like scalar values, strings, integers, booleans, and so on follow the pattern discussed above. But note that complex properties such as the Label's Font property use a different approach.

    You can find more detail and examples in this useful MSDN article.

  • Read this it's the best article I found. I Now understand what is ViewState.

    http://weblogs.asp.net/infinitiesloop/archive/2006/08/03/Truly-Understanding-Viewstate.aspx

0 comments:

Post a Comment