N
N. Demos
Hello,
I'm learning ASP.NET, and am having a strange problem with some example
code from the book I'm using. The code increments and displays the value
stored in a session variable when the "Add" button is clicked. In
addition, the session variable is reset to zero when the "Empty" button
is pressed.
The problem is if the value is non-zero and the page is reloaded the
value is incremented. It appears as if the "Add" onClick event handler
is fireing when the page reloads. I've tried various "If" statements to
shield the increment statment, but I haven't been able to stop it from
doing this. Can anyone tell me what's going on here, and how I can stop
it from incrementing on reload? I've included the code below. Any help
would be appreciated.
Thanks in advance,
N. Demos
Note: I have tried running this with and without declaring and
initializing the counter variable in the global.asax file, with no
change in behavior.
PAGE CODE:
------------------------
<%@ Page language="VB"%>
<SCRIPT language="vb" runat="server">
Sub EmptyClick(Sender as System.Object, E As System.EventArgs)
Session("BasketCount") = 0
End Sub
Sub AddClick(Sender as System.Object, E As System.EventArgs)
Session("BasketCount") += 1
End Sub
</SCRIPT>
<HTML>
<HEAD>
<LINK href="../General.css" rel="stylesheet">
<TITLE>Session Example</TITLE>
</HEAD>
<BODY>
<FORM id="BasketForm" runat="server">
<ASP:button id="btnEmpty" text="Empty" onClick="EmptyClick"
runat="server" />
<BR/>
<ASP:button id="btnAdd" text="Add" onClick="AddClick" runat="server" />
<BR/>
Basket Items: <%=Session("BasketCount") %>
</FORM>
</BODY>
</HTML>
I'm learning ASP.NET, and am having a strange problem with some example
code from the book I'm using. The code increments and displays the value
stored in a session variable when the "Add" button is clicked. In
addition, the session variable is reset to zero when the "Empty" button
is pressed.
The problem is if the value is non-zero and the page is reloaded the
value is incremented. It appears as if the "Add" onClick event handler
is fireing when the page reloads. I've tried various "If" statements to
shield the increment statment, but I haven't been able to stop it from
doing this. Can anyone tell me what's going on here, and how I can stop
it from incrementing on reload? I've included the code below. Any help
would be appreciated.
Thanks in advance,
N. Demos
Note: I have tried running this with and without declaring and
initializing the counter variable in the global.asax file, with no
change in behavior.
PAGE CODE:
------------------------
<%@ Page language="VB"%>
<SCRIPT language="vb" runat="server">
Sub EmptyClick(Sender as System.Object, E As System.EventArgs)
Session("BasketCount") = 0
End Sub
Sub AddClick(Sender as System.Object, E As System.EventArgs)
Session("BasketCount") += 1
End Sub
</SCRIPT>
<HTML>
<HEAD>
<LINK href="../General.css" rel="stylesheet">
<TITLE>Session Example</TITLE>
</HEAD>
<BODY>
<FORM id="BasketForm" runat="server">
<ASP:button id="btnEmpty" text="Empty" onClick="EmptyClick"
runat="server" />
<BR/>
<ASP:button id="btnAdd" text="Add" onClick="AddClick" runat="server" />
<BR/>
Basket Items: <%=Session("BasketCount") %>
</FORM>
</BODY>
</HTML>