Problem w/ session variable being incremented on reload

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>
 
N

N. Demos

Psycho,
Thanks for your response and suggestion. I tried the following, but this
did not work either.

If Page.IsPostBack Then
Session("BasketCount") += 1
End If

It is still incrementing on reload.

However, I think the incrementation is occuring somewhere other than the
AddClick handler. I put the following debug line in the handler

Sub AddClick(Sender as System.Object, E As System.EventArgs)
lblDebug.text = CStr(CInt(lblDebug.text) + 1)

If Page.IsPostBack Then 'Sender.ID = "btnAdd"
Session("BasketCount") += 1
End If
End Sub

and this label in the HTML

Debug: <ASP:label id="lblDebug" text="" runat="server" />

And got the following results:
Initial Page Load:
--------------------
Basket Items: 0
Debug: 0

First Add Button Click:
-----------------------
Basket Items: 1
Debug: 1

After Page Reload/Refresh:
--------------------------
Basket Items: 2
Debug: 1

From this, it seems that AddClick is not being called/fired at reload.
Any ideas?

Thanks again.
 
G

Guest

sorry, i didn't see that you make on addclick the incremental stuff.
so if you want to make just one time this incremental stuff you should use
viewstate

So OnLoad put something like this
If Not Page.IsPostBack Then
ViewState["test"] = false;
end

On click event make a test
If ViewState["test"] == fasle then
Session("BasketCount") += 1
ViewState["test"] = true
end

or test
if Session("BasketCount") < 1 then
Session("BasketCount") += 1
end
 
N

N. Demos

Psycho,
I tried the "ViewState" code you suggested, but got a compiler error.
I'm not familier with ViewState, yet. So I may have implemented it
incorrectly. Although looking at your code below (if I'm interpreting it
correctly), I don't think that this is how this should work. The Add
button should increment the counter everytime it's clicked not only on
the first time, but it should never be incremented when the page is
manually reloaded.

As I mentioned in my previous post, I don't think that the
Session("BasketCount") variable is being incremented in the AddClick
Handler when the page is manually refreshed. It seems like a side effect
that is occuring behind the scenes.

Again thanks for your time and attention.
Regards,
N. Demos

sorry, i didn't see that you make on addclick the incremental stuff.
so if you want to make just one time this incremental stuff you should use
viewstate

So OnLoad put something like this
If Not Page.IsPostBack Then
ViewState["test"] = false;
end

On click event make a test
If ViewState["test"] == fasle then
Session("BasketCount") += 1
ViewState["test"] = true
end

or test
if Session("BasketCount") < 1 then
Session("BasketCount") += 1
end



:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top