views:

49

answers:

1

Hi,

Let's say we have 2 usercontrols uc1 and uc2 in a Page. I set the Viewstate["name"]="John" in uc1. How do I access this Viewstate["name"] in uc2 control?

+2  A: 

You could create a public property on UC1 which can read/write the ViewState data you are trying to set & access UC1's property from UC2.

See here for an example of property backed by Control's ViewState.

From UC2, you could get a reference of UC1 by Page.Controls("myUC1Control") or using Page.FindControl("myUC1Control").

shahkalpesh
This will require creating instance of uc1 everytime the page is loaded. This can be done but I was just wondering if we could access it in some other way.
The instance of UC1 will get created irrespective of this (unless you are loading the UC1 dynamically). You could add the value to parent Page's viewstate so that it can be accessed by all the child controls of the page. IMO, that is not a good way to go.
shahkalpesh