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);
    }
}

 

Results 1 - 5 of 7 12
Posted on Mar 16, 2017 08:12 AM
YacharuP says:
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();
}
}
}

 replace above code with below

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

 


Posted on Mar 16, 2017 11:44 PM

@Indresh,

Though i change the code like yours, the if condition is not at all working! it always go to else condition.

If you try for the same then reply with a code example that will make more sense.


Posted on Mar 17, 2017 03:26 AM

Hi YacharuP,

Can you please explain your requirement in more detail exactly what you are trying to achieve if possible explain your requirement with an example?


Posted on Mar 17, 2017 05:46 AM

@Indresh,

I wanted to reduce the executions throughout different classes as i used two layer architecture for my project so when a user saves data i'm reloading  gridview so it moves to required classes to get data to avoid that i'm using ViewState. when first time page loads i'm storing temporarily in ViewState instead of Sessions because i need to reduce executions for particular page only


Posted on Mar 17, 2017 07:43 AM

Hi YacharuP,

YacharuP says:
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);
}
}

I have cross check your above code it’s correct. Because you are using viewstate on pageload event.