By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Russell wrote:
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

--

Ed Courtenay
[MCP, MCSD]
http://www.edcourtenay.co.uk
Nov 16 '05 #2

P: n/a
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.

--
Patrik L÷wendahl
www.cshrp.net - 'Elegant code by witty programmers'
"Russell" <ru************@hotmail.com> wrote in message
news:15**************************@posting.google.c om...
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

Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.