Page and Control ViewState

Discussion in 'ASP .Net Building Controls' started by Eric Sullender, Sep 18, 2003.

  1. I'm not sure if I'm missing something here, or not approaching this
    the right way, but here is my problem.

    I have a page Page.aspx that contains a user control Control.ascx.

    In the Page_Load of Page.aspx I have the following:

    ViewState[ "param1" ] = "foo";

    In Control.ascx I attempt to reference:

    string newString = (string) ViewState[ "param1" ];

    and the value is null. I set a breakpoint and did some debugging in
    VS.NET and found that ViewState (in the control) is empty, but I can
    reference Page.ViewState[ "param1" ] and see that it equals "foo".
    However, if I try to reference Page.ViewState in the code the compiler
    complains. Any ideas? All I'm really trying to do is set a variable
    that controls can access.
    Eric Sullender, Sep 18, 2003
    #1
    1. Advertising

  2. Eric Sullender

    webgenie Guest

    Hi, Eric
    The UserControl class has a property called 'Page'.
    this property returns a reference of parent page which containes itself.
    you save a string 'foo' on Page.ViewState property.
    because in Page_Load event, you write a code only ViewState property name.
    it means Page.ViewState property.

    and in control.ascx, you try to get a value from ViewState["param1"].
    but you has save a value in Page.ViewState property.
    So you can't get a value from ViewState property. because ViewState statement in Control.ascx file means UserControl.ViewState property.

    You can access to Page.ViewState property using Page property in UserControl class likes below.

    -- Control.ascx
    private void Page_Load(object sender, EventArgs e) {
    string newString = Page.ViewState["param1"];
    }

    - webgenie
    msn messenger :


    "Eric Sullender" <> wrote in message news:...
    I'm not sure if I'm missing something here, or not approaching this
    the right way, but here is my problem.

    I have a page Page.aspx that contains a user control Control.ascx.

    In the Page_Load of Page.aspx I have the following:

    ViewState[ "param1" ] = "foo";

    In Control.ascx I attempt to reference:

    string newString = (string) ViewState[ "param1" ];

    and the value is null. I set a breakpoint and did some debugging in
    VS.NET and found that ViewState (in the control) is empty, but I can
    reference Page.ViewState[ "param1" ] and see that it equals "foo".
    However, if I try to reference Page.ViewState in the code the compiler
    complains. Any ideas? All I'm really trying to do is set a variable
    that controls can access.
    webgenie, Sep 18, 2003
    #2
    1. Advertising

  3. "Eric Sullender" <> wrote in message
    news:...
    > I'm not sure if I'm missing something here, or not approaching this
    > the right way, but here is my problem.
    >
    > I have a page Page.aspx that contains a user control Control.ascx.
    >
    > In the Page_Load of Page.aspx I have the following:
    >
    > ViewState[ "param1" ] = "foo";
    >
    > In Control.ascx I attempt to reference:
    >
    > string newString = (string) ViewState[ "param1" ];
    >
    > and the value is null. I set a breakpoint and did some debugging in
    > VS.NET and found that ViewState (in the control) is empty, but I can
    > reference Page.ViewState[ "param1" ] and see that it equals "foo".
    > However, if I try to reference Page.ViewState in the code the compiler
    > complains. Any ideas? All I'm really trying to do is set a variable
    > that controls can access.


    Don't use ViewState for passing data between controls. Use Session, or maybe
    Context.Items.

    This problem is because when you read from ViewState, you're reading the
    ViewState as of the beginning of the current request. What you write into
    ViewState is what will be placed into the __VIEWSTATE hidden field at the
    end of the current request.
    --
    John Saunders
    Internet Engineer
    John Saunders, Sep 18, 2003
    #3
  4. Eric Sullender

    Teemu Keiski Guest

    In the UserControl Page.ViewState fails, because ViewState is protected
    property of the Page. And by default Page and User Control have separate
    ViewState collections.

    Good way is to implement a property in the user control (that can use
    ViewState to store it's value if specifically needed), that Page can access
    and again read/write as is needed or as John told, use Context.Items which
    is same request-specific collection for all objects that can access the
    current HttpContext.

    --
    Teemu Keiski
    MCP, Designer/Developer

    AspInsiders Member, www.aspinsiders.com
    ASP.NET Forums Moderator, www.asp.net
    AspAlliance Columnist, www.aspalliance.com


    "Eric Sullender" <> wrote in message
    news:...
    > I'm not sure if I'm missing something here, or not approaching this
    > the right way, but here is my problem.
    >
    > I have a page Page.aspx that contains a user control Control.ascx.
    >
    > In the Page_Load of Page.aspx I have the following:
    >
    > ViewState[ "param1" ] = "foo";
    >
    > In Control.ascx I attempt to reference:
    >
    > string newString = (string) ViewState[ "param1" ];
    >
    > and the value is null. I set a breakpoint and did some debugging in
    > VS.NET and found that ViewState (in the control) is empty, but I can
    > reference Page.ViewState[ "param1" ] and see that it equals "foo".
    > However, if I try to reference Page.ViewState in the code the compiler
    > complains. Any ideas? All I'm really trying to do is set a variable
    > that controls can access.
    Teemu Keiski, Sep 18, 2003
    #4
  5. Eric Sullender

    Rocky Moore Guest

    "Eric Sullender" <> wrote in message
    news:...
    > I'm not sure if I'm missing something here, or not approaching this
    > the right way, but here is my problem.
    >
    > I have a page Page.aspx that contains a user control Control.ascx.
    >
    > In the Page_Load of Page.aspx I have the following:
    >
    > ViewState[ "param1" ] = "foo";
    >
    > In Control.ascx I attempt to reference:
    >
    > string newString = (string) ViewState[ "param1" ];
    >
    > and the value is null. I set a breakpoint and did some debugging in
    > VS.NET and found that ViewState (in the control) is empty, but I can
    > reference Page.ViewState[ "param1" ] and see that it equals "foo".
    > However, if I try to reference Page.ViewState in the code the compiler
    > complains. Any ideas? All I'm really trying to do is set a variable
    > that controls can access.


    And don't forget about Cache.. Easy way to pass data and you can remove it
    just as easily when you use it else where on the page. In one site I have
    web user controls for each page of the site while the main page has the
    template that wraps the content and handles navigation. The controls
    themselves have a title of the page and I set a cache item in the control to
    the title of the page and in the <title> tag, I use the cached value and
    then clear it. Seems to work well.

    Rocky
    Rocky Moore, Oct 10, 2003
    #5
    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. Ben Rush
    Replies:
    2
    Views:
    507
    Ben Rush
    Dec 5, 2003
  2. Steph
    Replies:
    2
    Views:
    1,648
    heislord5
    May 11, 2011
  3. ujjc001
    Replies:
    0
    Views:
    1,363
    ujjc001
    Jul 27, 2005
  4. Josema
    Replies:
    2
    Views:
    341
  5. Josema
    Replies:
    0
    Views:
    200
    Josema
    May 9, 2005
Loading...

Share This Page