By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,322 Members | 1,904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,322 IT Pros & Developers. It's quick & easy.

Problem w/ session variable being incremented on reload

P: n/a
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>

--
Change "seven" to a digit to email me.
Nov 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
you can stop incermenting on reload if you test

Page.IsPostBack

http://msdn.microsoft.com/library/de...tBackTopic.asp

"N. Demos" wrote:
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>

--
Change "seven" to a digit to email me.

Nov 18 '05 #2

P: n/a
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.


Psycho wrote:
you can stop incermenting on reload if you test

Page.IsPostBack

http://msdn.microsoft.com/library/de...tBackTopic.asp

"N. Demos" wrote:

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>

--
Change "seven" to a digit to email me.

--
Change "seven" to a digit to email me.
Nov 18 '05 #3

P: n/a
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. Demos" wrote:
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.


Psycho wrote:
you can stop incermenting on reload if you test

Page.IsPostBack

http://msdn.microsoft.com/library/de...tBackTopic.asp

"N. Demos" wrote:

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>

--
Change "seven" to a digit to email me.

--
Change "seven" to a digit to email me.

Nov 18 '05 #4

P: n/a
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
Psycho wrote:
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. Demos" wrote:

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.


Psycho wrote:

you can stop incermenting on reload if you test

Page.IsPostBack

http://msdn.microsoft.com/library/de...tBackTopic.asp

"N. Demos" wrote:

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>

--
Change "seven" to a digit to email me.

--
Change "seven" to a digit to email me.

--
Change "seven" to a digit to email me.
Nov 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.