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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I keep a variable's value? 1

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
I have a form with 2 textboxes, a calendar, and 2 buttons.

When I click the first button, the calendar pops up and a variable called Cal is assigned the value of 1.

When I click the second button, the calendar pops up and a variable called Cal is assigned the value of 2.

When the calendar is clicked, it checks the value of Cal to see what textbox it should update with it's value - 1 or 2)

I tried this with built in code, and also code behind.
When I press either button, the form go's back to the server to display the calendar, and the value of Cal is lost.

Link9(Paul) told me I should be using code behind, so I created a .vb page and put all my code in it. It still does not keep the value of Cal. What am I doing wrong???

 
The codebehind bit was good advice, even if it didn't fix this particular issue. ;-)

I apparently misunderstood your question. Please see my reply in your original thread, and let me know.

:)
paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Thanks, Paul. I'll read the other post.
 
I think this answer needed a star, and I wanted to repost it here for everyone else to learn from.

The correct answer was:

If it's only needed on the same page, then you can place it into ViewState

to assign:
ViewState("varName") = varName

to retrieve:
varName = ViewState("varName")

or to save it from one page to another, either
(a)pass it via querystring (low resources, low security)
(b)store it in session (higher resources, higher security)

There are other ways, and I always suggest querystring whenever possible. Session should be used very sparingly to conserve resources, and only ever to store simple variables (ints, strings, etc...), never complicated objects (custom classes other than structs), which require more memory.

Session can be used to store these objects, but should not be used in that fashion.

As to why the variable isn't saved, ASP.NET is still a stateless medium, just like any web based technology is (and just like ASP Classic is).

ViewState is the resource equivalent to passing things via querystring, since they wind up in a hidden variable that's posted back just like anything else. The nice thing is that it's bundled quite nicely into the ViewState object automatically w/o any special gyrations on your part.

Let me know if this gets you going.

-paul

This works great!
 
Great tip about the viewstate. I didn't know you could do that. Thanks for the info!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top