Brock said:
Why do you need to do this?
I want all my pages to always be able to retrieve some detailed info
about its referer page: RawUrl and the PageTitle. Using this info, a
page can have a LinkButton with text=RefererPageTitle. When the "return
to referer" LinkButton is clicked, the code will Response.Redirect(
RawUrl of the referer ) ;
So I built a table, called a PageLoadMaster. In Page_Load, when
IsPostBack = false, I call a function to create a PageLoad row for the
page. That function stores the RawUrl and TitleText of the page in the
row and returns a guid key to the row. This guid, in string form, which
is called the LoadId, is stored in the StateBag ( fun name! ) of the
Page control.
string LoadId = LoadMaster.NewPage( Request, "Customer home" ) ;
ViewState["LoadId"] = LoadId ;
Now a user control on the page does a redirect to another page. I want
to pass to that page the LoadId of this refering page:
string LoadId = Parent.ViewState["LoadId"].ToString( ) ;
Response.Redirect( Url + "?RefererLoadId=" + LoadId ) ;
The assumption being that the Parent of a user control will always be a
Page control. ( I guess I could walk up the Parent tree of a control
until I came to a Page control. )
Typically the parent control would have a public
property exposing the data you need to get at. Or if it's simply some data
you control needs to persist across the postback you put it in your own ViewState
collection.
I guess. Playing with it, I was able to add a StateBag member to the
user control called "ParentStateBag".
public StateBag ParentStateBag ;
Then, in the Page_Load of the parent, set the ParentStateBag reference
to the ViewState of the page.
itemOrderGrid.ParentStateBag = ViewState ;
( not sure if this would always work or not. What if ASP.NET rebuilds
the StateBag of a page at some point. My user control will then
reference an old copy of the ViewState of its parent. )
I have to laugh at this encapsulation stuff. If I can get at the
StateBag of the parent this way, why not allow access thru the
ViewState property?
-Steve
In my user control I want to read the ViewState dictionary of the
Parent control. But this sensible idea is not permitted by the
compiler:
Compiler Error Message: CS1540: Cannot access protected member
'System.Web.UI.Control.ViewState' via a qualifier of type
'System.Web.UI.Control'; the qualifier must be of type
'ASP.ItemOrderGrid' (or derived from it)
Source Error:
Line 5:Trace.Write( "LoadId", Parent.ViewState["LoadId"].ToString( ))
;
Do I understand this correctly? The parent control of a user control
is the Page control? Basically, I want the user control to be able to
access the state bag of the page it is instantiated on. Is that
doable?
thanks,
-Steve