Thursday, 12 July 2012

how to use ViewState in asp.net

asp.net ViewState example: how to use ViewState in asp.net 

aspx :
 
<body>
    <form id="form1" runat="server">
    <div>
   
    </div>
    <asp:Label ID="lblCount" runat="server" Text=""></asp:Label>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </form>
</body>

 
 c# :

 protected void Button1_Click(object sender, EventArgs e)
        {
            int count = 1;
            if (ViewState["count"] == null)
            {
                count = 1;
            }
            else
            {
                count = (int)ViewState["count"] + 1;
            }
            ViewState["count"] = count;
            lblCount.Text = "Count = " + count;
        }




 

No comments:

Post a Comment