Accessing ViewState mutually between PageControl and UserControl.

In my current project, I needed to save a bool variable in UserControl’s ViewState and had to access that in the Parent Container Page. After some thought, here’s a refined approach on this task. To begin with, in asp.net webforms world, ViewState is every control’s own private small storage, usefull during postbacks, for small quantity of data.   
http://msdn.microsoft.com/en-us/library/system.web.ui.control.viewstate.aspx

System.Web.UI.Control  (has the ViewState property) 
   |
   ——>  System.Web.UI.Page  (Page Control has it’s own ViewState, which no other control can access)
   |
   —–>  System.Web.UI.UserControl (UserControl has it’s own ViewState, which no other control can access)   
Hence the Page control’s (aspx Page’s) ViewState can NOT be accessed in a UserControl directly and vice-versa. However if the ViewState information is exposed through public property, this gets easy. In this example below, the aspx Page is derived from a Base page class. The Base Page class has a public property which exposes the Page’s ViewState Value — which in turn can be accessed by the UserControl contained within that page. And similar, with UserControl too.

Base Page class

public class BasePage : System.Web.UI.Page
{
    public int ParentPageVSValue
    {
        get
        {
            if (ViewState["_ParentPageVSValue"] != null)
                return int.Parse(ViewState["_ParentPageVSValue"].ToString());
            else
                return 0;
        }
        set
        {
            ViewState["_ParentPageVSValue"] = value;
        }
    }
}

codebehind

public partial class ParentPage1 : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ParentPageVSValue = 89;
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    //Can not access UserControl's ViewState
    var t = ViewState["_Test1"]; //t is null

    //Can access UserControl's public property which
    //returns Viewstate value
    int y1 = TestUserControl11.Test1;
}
}

UserControl

public int Test1
{
    get
    {
        if (ViewState["_Test1"] != null)
            return int.Parse(ViewState["_Test1"].ToString());
        else
            return 0;
    }
    set
    {
        ViewState["_Test1"] = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Test1 = 3;
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    //returns null if you try to access Parent Page's ViewState
    var t = ViewState["_ParentPageVSValue"];  //t is null.

    //The public property returns the valid value.
    int y1 = ((BasePage)(this.Page)).ParentPageVSValue; 
}

2

That’s all about it. Thanks for reading.

Advertisements
This entry was posted in General ASP.Net C#. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s