user control cant access ViewState of Parent control

Discussion in 'ASP .Net' started by Steve Richter, Apr 5, 2005.

  1. 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
     
    Steve Richter, Apr 5, 2005
    #1
    1. Advertising

  2. Steve Richter

    Brock Allen Guest

    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
     
    Brock Allen, Apr 5, 2005
    #2
    1. Advertising

  3. 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
     
    Steve Richter, Apr 5, 2005
    #3
  4. Steve Richter

    Brock Allen Guest

    > 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
     
    Brock Allen, Apr 5, 2005
    #4
  5. 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
     
    Steve Richter, Apr 5, 2005
    #5
  6. Steve Richter

    Brock Allen Guest

    Brock Allen, Apr 5, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mariano Drago

    Cant get rid off viewstate

    Mariano Drago, Apr 8, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    511
    Mariano Drago
    Apr 9, 2004
  2. =?Utf-8?B?UmFlZCBTYXdhbGhh?=

    Access User Control DataGrid Controls from parent page

    =?Utf-8?B?UmFlZCBTYXdhbGhh?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    884
    IPGrunt
    Feb 16, 2005
  3. =?Utf-8?B?TG91aXNl?=
    Replies:
    6
    Views:
    470
  4. Nagaraj
    Replies:
    1
    Views:
    917
    Lionel B
    Mar 1, 2007
  5. Bitswapper
    Replies:
    5
    Views:
    183
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page