What is ViewState its Features Usage and Description in ASP.Net

Last Reply on Dec 05, 2012 01:52 AM By RkHirpara

Posted on Dec 05, 2012 01:22 AM


I used these code in my page




 string data = Server.UrlDecode(Request.QueryString["BehCode"]);

        _cmd.Parameters.AddWithValue("@image", filename);
        _cmd.Parameters.AddWithValue("@Behcode", data);
        _cmd.Parameters.AddWithValue("@id", Convert.ToInt32(ViewState["Id"]));

        int ID = Convert.ToInt32(_cmd.ExecuteScalar());
        if (ID > 0)
            ViewState["Id"] = ID.ToString();

I don't any thing about ViewState  what dose this code do?

can you explain about it

Best Regards


Posted on Dec 05, 2012 01:28 AM

Like a session, it is also a storage variable. I hope u are well favour with the session variable. But the only difference is viewstate is only with in that page. (i.e), if u used a viewstate["Test"], then "Test" is the variable that u can access only with in the page.

Using the viewstate is not adviceable because it increas the page wait. So if a application need to design with high speed and efficiency, using view state is not adviceable. In fact u can use hidden variable as a replacement.


Hope this may help u

Posted on Dec 05, 2012 01:52 AM Modified on on Dec 05, 2012 01:53 AM

Hi, Neda ViewState is used to matain value over PostBacks. It losts value outside the page. As you are using ViewState["id"] let say on page Page1.aspx.cs; from any point of the Page.aspx.cs you can access it once after setting the value till you reset the value., after resetting you the you will get the new value.

Even after PostBack it maitains the value. But once you left the Page1, It will no more available.

For further information please refer

ViewState Description