blog

Home / DeveloperSection / Blogs / EnableViewState in ASP.NET

EnableViewState in ASP.NET

Anchal Kesharwani7484 14-Aug-2014

In this blog, I’m explaining the concept of enableviewstate in asp.net.

EnableViewState or ViewStateMode is the property allows automatic state management feature enables server control to repopulate without writing any extra code. This feature is not free however, since the state of a control is passed to and from the server in a hidden form field. You should be aware of when ViewState is helping you and when it is not.

Example
Step 1

Write the code in EnableViewState.aspx:

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="EnableViewState.aspx.cs"Inherits="_Default"%>
<!DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
    <title></title>
    <styletype="text/css">
        th {
            text-align:left;
        }
        </style>
</head>
<body>
    <formid="form1"runat="server">
    <div>
        <asp:LabelID="lblControl1"runat="server"EnableViewState="false"></asp:Label>
        <br/>
             <asp:LabelID="lblControl2"runat="server"EnableViewState="true"></asp:Label>
        <br/>
         <asp:ButtonID="btnCheck"runat="server"Text="Check"  style="height: 26px"/>
        <br/>
        <br/>
       
     <asp:LabelID="lblMsg"runat="server"EnableViewState="true"Text="Before Post Back"></asp:Label>
    </div>
    </form>
</body>
</html>

Step 2

Run the application:

EnableViewState in ASP.NET

Step 3

After click on Check button:

EnableViewState in ASP.NET

In this example one label control disable EnableViewState and another label enable EnableViewState.


Updated 18-Sep-2014

Leave Comment

Comments

Liked By