473,782 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(Send er as System.Object, E As System.EventArg s)
Session("Basket Count") = 0
End Sub
Sub AddClick(Sender as System.Object, E As System.EventArg s)
Session("Basket Count") += 1
End Sub

</SCRIPT>

<HTML>
<HEAD>
<LINK href="../General.css" rel="stylesheet ">
<TITLE>Sessio n Example</TITLE>
</HEAD>
<BODY>
<FORM id="BasketForm " runat="server">
<ASP:button id="btnEmpty" text="Empty" onClick="EmptyC lick"
runat="server" />
<BR/>
<ASP:button id="btnAdd" text="Add" onClick="AddCli ck" runat="server" />
<BR/>
Basket Items: <%=Session("Bas ketCount") %>
</FORM>
</BODY>
</HTML>

--
Change "seven" to a digit to email me.
Nov 18 '05 #1
4 2520
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(Send er as System.Object, E As System.EventArg s)
Session("Basket Count") = 0
End Sub
Sub AddClick(Sender as System.Object, E As System.EventArg s)
Session("Basket Count") += 1
End Sub

</SCRIPT>

<HTML>
<HEAD>
<LINK href="../General.css" rel="stylesheet ">
<TITLE>Sessio n Example</TITLE>
</HEAD>
<BODY>
<FORM id="BasketForm " runat="server">
<ASP:button id="btnEmpty" text="Empty" onClick="EmptyC lick"
runat="server" />
<BR/>
<ASP:button id="btnAdd" text="Add" onClick="AddCli ck" runat="server" />
<BR/>
Basket Items: <%=Session("Bas ketCount") %>
</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("Basket Count") += 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.EventArg s)
lblDebug.text = CStr(CInt(lblDe bug.text) + 1)

If Page.IsPostBack Then 'Sender.ID = "btnAdd"
Session("Basket Count") += 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
initializin g 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(Send er as System.Object, E As System.EventArg s)
Session("Basket Count") = 0
End Sub
Sub AddClick(Sender as System.Object, E As System.EventArg s)
Session("Basket Count") += 1
End Sub

</SCRIPT>

<HTML>
<HEAD>
<LINK href="../General.css" rel="stylesheet ">
<TITLE>Sessio n Example</TITLE>
</HEAD>
<BODY>
<FORM id="BasketForm " runat="server">
<ASP:button id="btnEmpty" text="Empty" onClick="EmptyC lick"
runat="server " />
<BR/>
<ASP:button id="btnAdd" text="Add" onClick="AddCli ck" runat="server" />
<BR/>
Basket Items: <%=Session("Bas ketCount") %>
</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("Basket Count") += 1
ViewState["test"] = true
end

or test
if Session("Basket Count") < 1 then
Session("Basket Count") += 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("Basket Count") += 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.EventArg s)
lblDebug.text = CStr(CInt(lblDe bug.text) + 1)

If Page.IsPostBack Then 'Sender.ID = "btnAdd"
Session("Basket Count") += 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
initializin g 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(Send er as System.Object, E As System.EventArg s)
Session("Basket Count") = 0
End Sub
Sub AddClick(Sender as System.Object, E As System.EventArg s)
Session("Basket Count") += 1
End Sub

</SCRIPT>

<HTML>
<HEAD>
<LINK href="../General.css" rel="stylesheet ">
<TITLE>Sessio n Example</TITLE>
</HEAD>
<BODY>
<FORM id="BasketForm " runat="server">
<ASP:button id="btnEmpty" text="Empty" onClick="EmptyC lick"
runat="server " />
<BR/>
<ASP:button id="btnAdd" text="Add" onClick="AddCli ck" runat="server" />
<BR/>
Basket Items: <%=Session("Bas ketCount") %>
</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("Basket Count") 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("Basket Count") += 1
ViewState["test"] = true
end

or test
if Session("Basket Count") < 1 then
Session("Basket Count") += 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("Basket Count") += 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.EventArg s)
lblDebug.text = CStr(CInt(lblDe bug.text) + 1)

If Page.IsPostBack Then 'Sender.ID = "btnAdd"
Session("Basket Count") += 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.IsPostB ack

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
initializin g 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(Send er as System.Object, E As System.EventArg s)
Session("Basket Count") = 0
End Sub
Sub AddClick(Sender as System.Object, E As System.EventArg s)
Session("Basket Count") += 1
End Sub

</SCRIPT>

<HTML>
<HEAD>
<LINK href="../General.css" rel="stylesheet ">
<TITLE>Sessio n Example</TITLE>
</HEAD>
<BODY>
<FORM id="BasketForm " runat="server">
<ASP:button id="btnEmpty" text="Empty" onClick="EmptyC lick"
runat="serv er" />
<BR/>
<ASP:button id="btnAdd" text="Add" onClick="AddCli ck" runat="server" />
<BR/>
Basket Items: <%=Session("Bas ketCount") %>
</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
5039
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 this on the page. However after 22 minutes, or some interval longer than the session timeout, the Session.SessionID values is still availible. What I want to achieve is this. There is a small section of my site that must have an active session....
5
1674
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 working well for a couple of days. But, after a while everytime I reload the shoppingbasket I see different products which I've never added. So it seems like I'm seeing the basket of another customer. As it seemed IIS was 'sharing' sessions...
3
5976
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 the value of that session cookie in response to a button click in a form, without resubmitting the page. For some reason, the following doesn't seem to work:
6
1585
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: http://www.dwayneepps.com/abr_site/fundingrequest.asp If you click on the "New Transaction" button it will generate a new row for the user to input information. I call a javascript function when the user clicks the "New Transaction" button that redirects to another page that...
0
1452
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
1566
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 decrement to facilitate a movenext and movelast. I have been pulling my hair out and am at the end of my rope. PLEASE, how should I implement this? I have tried everything I can think of, which really isn't much considering I am just beginning...
14
2933
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 relies on about 40 sesion variables and a mixture of vbs and js files. One of the session variables is an id number that gets incremented to point to the next piece of data to be displayed. This variable is initialised on the first page of the site...
4
4084
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 have to click it twice to do the job (the first submit resets the sessions but this it too late to change the field values, which requires another submit to pick up the new session values). Problem is I cant think how to accomplish the resetting of...
1
1519
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 result.Can u please enlignten me what i can do such that when I reload my page the sessoion variable isn't incremented? I'll greatly appreciate any help..Thanks
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10076
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.