WebForm variable data lost

pitaridis

Well-known member
Joined
Nov 18, 2005
Messages
63
Programming Experience
10+
When I declare a variable in a form, every time I click a button, the value of the variable becomes 0. I have an example below. Can someone tell me what's wrong with the following code???

<%@ PageLanguage="VB" %>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
scriptrunat="server">
Shadows a AsNewInteger
ProtectedSub Button1_Click(ByVal sender AsObject, ByVal e As System.EventArgs)
a += 1
Label1.Text = a
EndSub
</
script>
<
htmlxmlns="http://www.w3.org/1999/xhtml">
<
headrunat="server">
<title>Untitled Page</title>
</
head>
<
body>
<formid="form1"runat="server">
<div>
<asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Button"/>
<asp:LabelID="Label1"runat="server"Text="Label"></asp:Label></div>
</form>
</
body>
</
html>
 
a += 1

a += 1 means that I want to add one to the variable a. I used it in order to find out why the program does not increase by one the variable. In VB it works fine. The probles happenes when I try to do exactly the same thing in asp.net.​
 
pitaridis (and tchavez), you should have a read about managing state in asp(.net) pages. check up on "session", "application", "cookie" and "cache".
 
It is the same thing

Programaticaly it is the same thing, the advantage of using
a += 4
instead of

a = a + 4
is that it is a bit faster.

My problem is not the syntax of increasing a value of a variable but how can I reuse the value of a variable after a reload of the page. Currently I use an invisible textbox, in which I store some value, so that the next time I will be able to retrieve the value.
 
I can not understand why I should manage the whole thing. So what is the reason of using global variables in an asp.net application if they do not remember their values???


pitaridis (and tchavez), you should have a read about managing state in asp(.net) pages. check up on "session", "application", "cookie" and "cache". ????
If I use sessions it will cause problem because other windows of the sessions could change it's value. The same


 
How can the application to remember the properties of various objects like the contents of a label, and it can not remember the value of a variable. Somehow there should be a way to do it.
 
Back
Top