user control cant access ViewState of Parent control

S

Steve Richter

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
 
B

Brock Allen

Why do you need to do this? Typically the parent control would have a public
property exposing the dat 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.

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
S

Steve Richter

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
-Brock
DevelopMentor
http://staff.develop.com/ballen


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
 
B

Brock Allen

I want all my pages to always be able to retrieve some detailed info
about its referer page: RawUrl and the PageTitle.

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. )

The page is always accessible directly via the Page property. So then in
your control why don't you call Page.Request.UrlReferrer? :)

-Brock
DevelopMentor
http://staff.develop.com/ballen
 
S

Steve Richter

Brock said:
The page is always accessible directly via the Page property. So then in
your control why don't you call Page.Request.UrlReferrer? :)

I just checked that ( trace.write is great! ) and it does contain the
RawUrl of the referrer. When I checked into UrlReferrer last week I
could have sworn it did not contain the QueryString. What is the
emoticon for "stupid face"?

But I still like the idea of storing the info in a table. That way I
can always get at extra info like the PageTitle and place it as the
text on the return link.

thanks!

-Steve
 

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

Top