Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ViewState 1

Status
Not open for further replies.

Craftor

Programmer
Feb 1, 2001
420
NZ
Hi all

How does one use the ViewState property? I need to persist a variable setting through a page repost and I have put the following code in the CLICK event of a button:

ViewState["ErrorState"] = "username";

When the page reloads, I use the following code:

if (Page.IsPostBack == true)
errorstate = (string)ViewState["ErrorState"].ToString();

and it gives me the following error:

Object reference not set to an instance of an object.

Now --- what am I doing wrong here??

Thanks as always

Craftor :cool:

 
thats C# right?

Not sure if its all that different syntax wise from vb.net, but here's the main ways to get to viewstate info:

to add it, you use
Viewstate.Add(Key as string, Value as string)


to get or assign a value you access
Viewstate.Item(Key)


Try incorporating these into your code when you assign the value/create the variable in viewstate, and hopefully it'll fix the error.
:)

hth

D'Arcy
 
jfrost - thank you sooo much :) - all I was missing was that code to initialise the ViewState key!

Works like a charm now ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top