?
Solved

Button is one click behind on updating session variable.

Posted on 2008-06-15
7
Medium Priority
?
299 Views
Last Modified: 2013-11-26
The problem I am having is that I need to track a number on an asp.net website.  When a forward and backward button is clicked, the number should increase or decrease by one.  The result I am getting is that when the button is clicked the first time nothing happens.  Then the second time it is clicked the textbox displaying the current value of the session variable is finally updated. Another thing that happens is if I click the add button twice then click the minus button....the number will continue to increment.  Its like the displayed value is one click behind.  

What do I need to do to update the text box immediately after the add/minus button is clicked and not be a click behind?

I am using VS 2008.

Thanks for the help.
Partial Class _Default
    Inherits System.Web.UI.Page
 
    
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
        If Not Page.IsPostBack Then
            Session("counter") = 1
        Else
 
        End If
        TextBox1.Text = Session("counter")
 
    End Sub
 
    Protected Sub btnSubtract_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
        Session("counter") -= 1
    End Sub
 
    
    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Session("counter") += 1
    End Sub
End Class

Open in new window

0
Comment
Question by:RocketSauce
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 5

Expert Comment

by:Qaiser_Mehmood_Mughal
ID: 21791429
I think Firstly you have to convert the value to int to do any subtraction/addition then also
TextBox1.Text = Session("counter")
you above line of code in button's events.
0
 

Expert Comment

by:victor79
ID: 21791922
try

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Session("counter") = 1
            TextBox1.Text = Session("counter")
        End If
 End Sub

Protected Sub btnSubtract_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubtract.Click
        TextBox1.Text -=1
End Sub
   
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        TextBox1.Text +=1
End Sub
0
 
LVL 2

Author Comment

by:RocketSauce
ID: 21793861
Thanks for the comments so far.  Thanks yes it is the way to go to convert strings to integers/etc when doing calculations.  
@Victor79 - your solution does sync the value of the text box with the buttons.  But... Is there not a way to keep track of this value without storing the value on a control?  My plans will be to remove the text box completely once I figure this out...this won't be a number displayed on the page or to the end user.  

Using the same logic i have posted...I have also tried cache, and query strings and the same result happens where the value displayed in the textbox is one click behind.  
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Accepted Solution

by:
prairiedog earned 2000 total points
ID: 21794632
RocketSauce:
Your code is ok, as far as increasing/decreasing the counter is concerned.

The reason the textbox's value is updated one click behind is that you put TextBox1.Text = Session("counter") in the wrong place. Here is what your code is doing:
1. When page_load is fired the first time the page is accessed
       Session("counter") = 1
       TextBox1.Text = 1
2. When Add button is clicked, page_load is fired again. But since it is a postback, so If/End If block is skipped in the Page_Load, but TextBox1.Text = Session("counter") will still be executed, so now
      TextBox1.Text = Session("counter") = 1
After Page_Load, Add Button's event handler is fired, which will increase Session("counter") by 1, so now you have:
      Session("counter") = 2
But remeber TextBox1.Text is still 1. Do you see the problem now? Because you put TextBox1.Text = Session("counter") in the Page_Load event handler, so it will be always executed before Session("counter") -= 1 or Session("counter") += 1, that caused the delay.
0
 
LVL 2

Author Comment

by:RocketSauce
ID: 21795069
Thanks prairiedog.  You explained what I was doing wrong very nicely.  It looks like you gave me a push so I could reason the rest out on my own.  Take a look at the code below and see if that is the right way to do it.  If thats what you intended the final code to look like..I'll give you all your points then.  And thank you so much.  

Also...I was in a rush trying it out....code below doesn't have the buttons and stuff named properly and no ctype() string conversion going on...but you can see the logic and it compiles and works.



Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Session("counter") -= 1
        TextBox1.Text = Session("counter")
    End Sub
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Session("counter") = 1
            TextBox1.Text = Session("counter")
        End If
        
 
    End Sub
 
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Session("counter") += 1
        TextBox1.Text = Session("counter")
    End Sub
End Class

Open in new window

0
 
LVL 22

Expert Comment

by:prairiedog
ID: 21795685
It looks fine to me. As long as you keep it in mind the order a page is processed, you will know when and where to access the session variable, for example, Page_Load is always fired, no matther it is a page load or post back, then the event handler that causes the post back fires afterwards.
0
 
LVL 2

Author Comment

by:RocketSauce
ID: 21796185
Thanks guys.  I appreciate all of you posting your help.
0

Featured Post

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question