473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Problem w/ session variable being incremented on reload

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
4 2495
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David B | last post by:
Hi, Could somebody please tell me how I check whether a session is dead based soley on the Session.SessionID. At present the timeout is set to 20 minutes in IIS, I am aware that I can reset...
5
by: Ronald | last post by:
Hi there! I have a website partly written in ASP in where a shoppingbasket is used. The products of the shoppingbasket are stored in the session-object. (We use IIS6 on Windows 2003). This was...
3
by: M Wells | last post by:
Hi All, Just wondering how you go about changing the value of a session cookie via javascript? I have a PHP page that sets a session cookie when it first loads. I'd like to be able to change...
6
by: -D- | last post by:
I'm trying to accomplish the following. I'm trying to get the values for the table rows that are dynamically created to persist through a redirect. Referring URL:...
0
by: Alexander Widera | last post by:
hello all, i have a problem ... like I already discussed in the thread "session empty" I have the following problem: I created a completely new web... i added 2 files: sessiontest1.aspx:
3
by: Aaron | last post by:
Why do my session values return to nothing on post back? I want to click a button and have the row, as in: dataset.table(0).rows(THIS ROW NUMBER IS WHAT I AM TALKING ABOUT), either increment or...
14
by: Martin Walke | last post by:
Hi all, Is there any limit to the number of session variables a site can have? And is that affected by global.asa at all? The reason why i ask is that i have a relatively simple site that...
4
by: Ian Davies | last post by:
Hello I am struggling for a solution to clear some fields on my webpage that takes their values from some sessions My solution below works when the button is clicked twice. I sort of know why I...
1
by: phpuser123 | last post by:
I have a page that increments my session everytime,my passowrd and username are not correct..Buteven when I am reloading my page,the session is beinng incremented and as such I amgetting a defective...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.