Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't i access page viewstate in usercontrol?

Tags:

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.

like image 820
Rohit Raghuvansi Avatar asked Jun 11 '10 06:06

Rohit Raghuvansi


People also ask

Can ViewState be accessed in another page?

You can't access ViewState of one page from another page directly. If you want to access a particular ViewState value then you can pass the value in Context collection and then access the value in other page.

How do I view ViewState?

To use the ViewState property, the ASP.NET Web page must have a form element that has the attribute runat="server". To save a value to view state, create a new item that contains the value to save and add the item to the view state dictionary.

What is ViewState when ViewState is available?

View State is the method to preserve the Value of the Page and Controls between round trips. It is a Page-Level State Management technique. View State is turned on by default and normally serializes the data in every control on the page regardless of whether it is actually used during a post-back.

Where does ViewState value is stored?

Viewstate is stored on page it self in encoded form. You can't access the viewstate in client side in a direct manner. You need to know the encoding/decoding algorithms to fetch the valuable data from this viewstate in clientside code. You can use hidden variable to store data that will be used only on that page.


3 Answers

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.

like image 111
peanutbutter_lou Avatar answered Oct 22 '22 02:10

peanutbutter_lou


I still do not grok everything here (see my comments above). But I am pretty sure you are misunderstanding ViewState.

ViewState is per control, not per request or session or whatever.

In your example, consider some other control (e.g. a standard ASP.NET control) that for some reason decided to put something with a "name" of WorkFlow into viewstate. If what you are trying to do would work, this object would overwrite yours (or the other way around, yours would be overwritten by the other one).

Or am I missing something?

Maybe reading TRULY Understanding ViewState could help you understand what viewstate is/how it works (yes, I really like this article, this is why I keep posting that link).

like image 43
scherand Avatar answered Oct 22 '22 01:10

scherand


On postback did you create the control? If the code behind hasn't created the ctrl then it won't know about it.

only applicable if this is a generated control. You may need to post code and more info to get a propper answer.

Viewstate is a monster which is why a lot of us are going to MVC.

like image 37
griegs Avatar answered Oct 22 '22 00:10

griegs