473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Accessing ViewState in User Control

My ASP.NET 1.1. app dynamically loads a user control into a PlaceHolder
control that exists in an aspx page. The user control is loaded during the
Page_Load event of the aspx. The PlaceHolder's EnableViewState property
value is "true".

In the user control's Page_Load event procedure I want to read/write to the
ViewState.

Writing to the ViewState, like this, seems to be no problem:
this.ViewState["MyKey"] = someValue;

But attempting to read the value back from the ViewState causes an exception
to be thrown: "Object reference not set to an instance of an object"
someValue = this.ViewState["MyKey"].ToString();

I threw this.TrackViewState(); in the user control's Page_Load event
procedure to see if that was required, but apparently no dice.

What must I do to be able to use ViewState of the user control? Note that I
don't really care if I'm using the ViewState of the user control or of the
page that hosts the user control (I just don't want to store the value in
the Session).

Thanks!
Nov 19 '05 #1
3 8065
I'm using it, no problem, but with a difference. I have a panel on the page
and dropped the control into the panel in design view of the page (for
positioning) instead of dynamically loading it into a PlaceHolder.

"Jordan" <A@B.com> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
My ASP.NET 1.1. app dynamically loads a user control into a PlaceHolder
control that exists in an aspx page. The user control is loaded during the
Page_Load event of the aspx. The PlaceHolder's EnableViewState property
value is "true".

In the user control's Page_Load event procedure I want to read/write to
the ViewState.

Writing to the ViewState, like this, seems to be no problem:
this.ViewState["MyKey"] = someValue;

But attempting to read the value back from the ViewState causes an
exception to be thrown: "Object reference not set to an instance of an
object"
someValue = this.ViewState["MyKey"].ToString();

I threw this.TrackViewState(); in the user control's Page_Load event
procedure to see if that was required, but apparently no dice.

What must I do to be able to use ViewState of the user control? Note that
I don't really care if I'm using the ViewState of the user control or of
the page that hosts the user control (I just don't want to store the value
in the Session).

Thanks!

Nov 19 '05 #2
I've done this just like you describe.
In my case I found that for dynamically loaded controls I had to load
them every time the page loaded, and set the ID property of the control
to the same (arbitrary) value every time so that things stayed in sync.

So in VB I had
PlaceHolder.Controls.Clear()
PlaceHolder.Controls.Add(myControl)
myControl.ID = "abc"

I think that with multiple controls the last line is needed to keep
viewstate in sync with individual controls.

Nov 19 '05 #3
You can't access the viewstate once your control starts to render, as it
will render the new viewstate. You can read the value back at any stage
before that, the PreRender event is a good place to do this.

Cathal
"Jordan" <A@B.com> wrote in message
news:Oe**************@tk2msftngp13.phx.gbl...
My ASP.NET 1.1. app dynamically loads a user control into a PlaceHolder
control that exists in an aspx page. The user control is loaded during the
Page_Load event of the aspx. The PlaceHolder's EnableViewState property
value is "true".

In the user control's Page_Load event procedure I want to read/write to the ViewState.

Writing to the ViewState, like this, seems to be no problem:
this.ViewState["MyKey"] = someValue;

But attempting to read the value back from the ViewState causes an exception to be thrown: "Object reference not set to an instance of an object"
someValue = this.ViewState["MyKey"].ToString();

I threw this.TrackViewState(); in the user control's Page_Load event
procedure to see if that was required, but apparently no dice.

What must I do to be able to use ViewState of the user control? Note that I don't really care if I'm using the ViewState of the user control or of the
page that hosts the user control (I just don't want to store the value in
the Session).

Thanks!

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
2
by: LarryB | last post by:
Ok. Here is the question. I am writing code to make a multipage form. Each page is loaded from a usercontrol. Now when I get to the final page I would like to access the information on the...
1
by: Simon | last post by:
Hi everyone, I have a quick question that I hope someone can help me with: I've made a user control that contains a text box and some validation functionality. This control has a few extra...
5
by: Steve Richter | last post by:
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...
6
by: John | last post by:
Hi all, I am using dynamic user controls within my web app and these controls are loaded into placeholders via the LoadControl method. My problem is this: I have usercontrolA loaded into a...
10
by: Robert | last post by:
I have an app that was originally 1.1, now migrated to 2.0 and have run into some sporadic viewstate errors...usually saying the viewstate is invalid, eventvalidation failed or mac error. My web...
6
by: hitendra15 | last post by:
Hi I have created web user control which has Repeater control and Linkbutton in ItemTemplate of repeater control, following is the code for this control On first load it runs fine but when...
1
by: jelle.huygen | last post by:
Hello, I have a problem in ASP.NET 2.0 with the viewstate of my dynamically added user control. I have reproduced the problem with a very simple user control and a very simple page. On my...
12
by: Nick C | last post by:
Hi How can i reduce the viewstate for my asp.net application. It is getting very large now. What is a good solution? thanks N
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.