User control with in a parent aspx page cant see the view state

  • Thread starter Thread starter Russell
  • Start date Start date
R

Russell

Morning,

I have been having trouble with a my embedded user control not being
able to see the view state that I set in its parent page.

I have done a test with the following and I still cant get it to work!

PARENT PAGE (default.aspx):

private void Page_Load(object sender, System.EventArgs e)
{
int nPageID = 7;

ViewState["test1"] = nPageID;

//or
//ViewState.Add("test1", nPageID);


}

EMBEDDED USER CONTROL:

private void Page_Load(object sender, System.EventArgs e)
{
try
{
int test = (int)ViewState["test1"]);
}
}

when int test = (int)ViewState["test1"]); is run I get object not set
to an instance of an object.

What do I need to do so that my embedded user control can see the view
state set in my parent page?

thanks
RuSs
 
Russell said:
What do I need to do so that my embedded user control can see the view
state set in my parent page?

thanks
RuSs

ViewState is a protected property, which to cut a long story short,
means that it can only be accessed from the parent page.

If you want to pass values from the parent page to the user control, add
properties to the user control and set them from the parent page.

HTH
 
Hi Russell,

the viewstate is control specific. The member is a protected property so you
can only access it from within your control (a page is a control too) or
controls derived from your control.

To share information between the page and a control, use properties on the
control, sessions state or public members on the page.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top