Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > user control cant access ViewState of Parent control

Reply
Thread Tools

user control cant access ViewState of Parent control

 
 
Steve Richter
Guest
Posts: n/a
 
      04-05-2005
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

 
Reply With Quote
 
 
 
 
Brock Allen
Guest
Posts: n/a
 
      04-05-2005
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



> 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




 
Reply With Quote
 
 
 
 
Steve Richter
Guest
Posts: n/a
 
      04-05-2005

Brock Allen wrote:
> 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


 
Reply With Quote
 
Brock Allen
Guest
Posts: n/a
 
      04-05-2005
> 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



 
Reply With Quote
 
Steve Richter
Guest
Posts: n/a
 
      04-05-2005

Brock Allen wrote:
> > 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?


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

 
Reply With Quote
 
Brock Allen
Guest
Posts: n/a
 
      04-05-2005
> What is the emoticon for "stupid face"?

:S

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM
ViewState got confused between two user controls in same parent ct =?Utf-8?B?TG91aXNl?= ASP .Net 6 10-12-2005 06:53 PM
Page load of the parent page called twice when a modal dialog is opened from a button click of the user control on the parent page Samy ASP .Net 2 08-15-2005 03:30 PM
Access User Control DataGrid Controls from parent page =?Utf-8?B?UmFlZCBTYXdhbGhh?= ASP .Net 1 02-16-2005 04:44 PM



Advertisments