Page and Control ViewState

E

Eric Sullender

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

webgenie

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 : (e-mail address removed)


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

John Saunders

Eric Sullender said:
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.
 
T

Teemu Keiski

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
 
R

Rocky Moore

Eric Sullender said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top