473,244 Members | 1,293 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,244 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 2487
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.