Search This Blog

Saturday, 21 November 2015

ASP.NET How to use ViewState example program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        int clickscount = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TextBox1.Text = "0";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ViewState["cicks"] != null)
            {
                clickscount = (int)ViewState["cicks"] + 1;
            }
            TextBox1.Text = clickscount.ToString();
            ViewState["cicks"] = clickscount;
        }
    }
}

Note:

Clicks Button now, and the value gets incremented every time we click, so how is this possible now?
it's possible because, we are using the viewstate variable clicks to preserve the data between requests and response between the client and the webserver.


No comments:

Post a Comment