Why can’t i access page viewstate in usercontrol

asp.net

I stored a object in viewstate on Page. Now when i access the same viewsate object on usercontrol,it shows as null. I even tried creating the same viewstate with same name in usercontrol and page.Both holds different value.

I understand that viewstate is a protected property. How does this thing implement in above scenerio or is there any other reason for this behaviour.

Edit:

Usercontrol is there in the page markup. I am not loading it dynamically.

I have a page EditFacilityworkType.aspx. On page I have a usercontrol FacilityWorkTypeDetails.aspx(FacilityWorkTypeDetails1). Inside this usercontrol i have a user control Workflow.aspx(Workflow1)

Page_Load() of Page
I am retrieving workflowdetails on page_load() of page.

 FacilityWorktype facilityWorkType = facilityDetails.GetFacilityWorktypeDetail(SessionHelper.FacilityWorkTypeID);
 ViewState["WorkFlow"] = facilityWorkType.FacilityWorkTypeWorkFlow

Inside usercontrol FacilityWorkTypeDetails.aspx. I have a property

 public FacilityWorktype FacilityWorkTypeDetails
{
    get
    {
        #region Fill FacilityWorktype
        return GetEntityFromControl();
        #endregion
    }
    set
    {
        PopulateControls(value);
    }
}

Now i set this property in page load of page

FacilityWorkTypeDetails1.FacilityWorkTypeDetails = facilityWorkType;

Inside Workflow.aspx, I have a property

/// <summary>
/// Property to fill entity object from controls on this page
/// </summary>
public WorkFlow WorkFlowDetails
{
    get
    {
        return GetEntityFromControls();
    }
    set
    {            
        BindTranscriptionMethodDDL(ddlTranscMethod);
        PopulateControls(value);
    }
}

Now PopulateControls() of FacilityWorkTypeDetails1, i am setting property of workflow1

private void PopulateControls(FacilityWorktype value)
{

    Workflow1.WorkFlowDetails = value.FacilityWorkTypeWorkFlow;
}

Now when i am retrieving values from

 private WorkFlow GetEntityFromControls()
 {
     WorkFlow workFlow = (ViewState["WorkFlow"] as WorkFlow) ?? new WorkFlow();  
     //workFlow  is null

 }

So now inside this function workFlow is null. I want to ask,why is it null when i have set viewstate in page.

Best Answer

Scherand is very correct here. I'd like to add to what he has brought to the table.

Every control that derives from System.Web.UI.Control has the ViewState property. Under-the-hood the property is a StateBag collection. Every instance of a Control has its own StateBag for ViewState, so as Scherand mentioned, ViewState is unique to the control. When the page gets rendered, the entire Control tree of the Page is iterated, all ViewState collections are merged into a tree-like structure and that final structure is serialized to a string and rendered to the page.

Because the ViewState property is marked as protected, you can't get to the Page's ViewState from your User Control without the use of reflection.

But, in all honesty, you should abandon the use of ViewState as a data storage medium. Here are some reasons why:

  1. ViewState gets rendered and output to the client browser. Maintaining data objects in the collection bloats your Page's output.
  2. Unless you have encryption enabled on your ViewState, the encoded string that is rendered to the client browser can be decoded manually and simply anyone can access the content of your data objects. This is a rather significant security vulnerability.

It really sounds like all you want to do is share data between your Page and User Controls. The best way to share data between controls is to make use of the "Items" collection (which is a property of the HttpContext class). The collection is a Hashtable and can be accessed from your Page and User Controls like so:

Context.Items["Workflow"] = workflowInstance;

The best part of using this technique is that it doesn't incur any additional overhead or bloat the Page output. The Items collection exists in the context of a single HTTP request. This means that when your request is done and your Page's output has been rendered to the client browser, the Items collection is cleared from server memory. It's the ideal medium for temporary data storage within ASP.NET.

Now, if you want your data objects to remain accessible for more than just the current request, you'd be better off storing the objects in Session.

Related Topic