How to use ViewState for class objects using C# in ASP.Net

Last Reply on Mar 20, 2017 03:33 AM By Indresh

Posted on Mar 16, 2017 07:50 AM

Hi Guys,

I'm Working with ViewState in my project to reduce the cycles of data loading again and again.

It's not working! Please help me

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if ((object)ViewState["GridView"] != null)
        {
            global_functions.load_gridview(grd_master_district, (object)ViewState["GridView"]);
        }
        else
        {
            load_Grid_View();
        }
    }
}

private void load_Grid_View()
{
    try
    {
        Object obj = ClassBO.load_Master_District();
        ViewState["GridView"] = obj;
        global_functions.load_gridview(grd_master_district, obj);
    }
    catch (Exception ex)
    {
        ex.Message.ToString();
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert", "alert('Something Went Wrong!.')", true);
    }
}

 

You are viewing reply posted by: Indresh on Mar 20, 2017 03:33 AM.
Posted on Mar 20, 2017 03:33 AM Modified on on Mar 20, 2017 03:36 AM
YacharuP says:
ViewState is cleared after page refreshes?

Your above statement is correct. Because viewstate page class will hand over the posted back data to only those controls which implement IPostBackEventHandler.For more information please refer the below link.

ViewState and Postback