Shortcuts

Variable Scope In ASP.Net - PostBacks

Is there a way for variables to retain their values across postbacks on the same page? My code looks like this:

<script language="VB" runat="server">

Dim intLastStepCompleted As Integer

Sub Page_Load(Sender As Object, E As EventArgs)

' Code

End Sub

Sub custom_one()

' Code

End Sub

' Etc.

</script>

I have several data validation steps behind the Page_Load sub, and I use the variable "intLastStepCompleted" to keep track of how far the user has gotten. However, it doesn't seem to retain its value after each time the user submits the form.

I know I could use session variables, but I'm trying to get away from over-utilization of them, especially since I don't need to use the variable on any other pages.

Thanks.

"I'm a paladin with 18 charisma and 97 hit-points. I can use my Helm of Disintegration and do one d4 damage as my half mage elf wields his +5 Holy Avenger."

Re: Variable Scope In ASP.Net - PostBacks

augustwind

Check out 'Viewstate'

Thanks, that seems to do what I need.

Some of the info says that overuse of ViewState could have a negative effect on performance. Unfortunately I couldn't find anything that says how much data is too much. I'm storing about 25 fields' worth of data, all strings, using this method. Would that
pose a problem with multiple simultaneous users?

"I'm a paladin with 18 charisma and 97 hit-points. I can use my Helm of Disintegration and do one d4 damage as my half mage elf wields his +5 Holy Avenger."

Re: Variable Scope In ASP.Net - PostBacks

Why do you need to save 25 fields? You said you have a variable intLastStepCompleted. I would think that is the only value you should have to store in the viewstate. If you are not doing anything different and simply showing your form again, the values should
already be filled out. They should be ready for you to use at whatever step you left off on for validation.

Re: Variable Scope In ASP.Net - PostBacks

aspwhiz

Why do you need to save 25 fields? You said you have a variable intLastStepCompleted. I would think that is the only value you should have to store in the viewstate. If you are not doing anything different and simply showing your form again, the values should
already be filled out. They should be ready for you to use at whatever step you left off on for validation.

Sorry for the confusion. I was using that as merely an example. The way the code works is this (it's for a hotel reservation page):

2) Validate user input for completeness, etc. If fail, go back to step 1. If pass, hide user form and display a recap of the user's entries.

3) Recap page has a "Submit Reservation" button. Once user clicks that button, insert a new record into a database using the 25 variables collected in step 1.

This is all being done on one page using PostBacks. The problem that precipitated this question is the fact that when I store the user inputs into variables in step 1, those variables lose their values after each PostBack, so I can't use them to feed my
SQL statement for step 3.

I hope that better explains the context of my question.

"I'm a paladin with 18 charisma and 97 hit-points. I can use my Helm of Disintegration and do one d4 damage as my half mage elf wields his +5 Holy Avenger."