Results 1 to 6 of 6

Thread: click the button twice ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99

    Unhappy click the button twice ?

    Hello everybody,


    Can anyone please explain me a solution or workaround for my following problem ?

    When I want to pass a session variable with a postback, then I always need to click the button twice !

    Here's my code :

    VB Code:
    1. Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    2.         'CODEGEN: This method call is required by the Web Form Designer
    3.         'Do not modify it using the code editor.
    4.  
    5.         Response.Write(Session("myTest"))
    6.         InitializeComponent()
    7.     End Sub
    8.  
    9. #End Region
    10.  
    11.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    12.         Session("myTest") = "testing"
    13.     End Sub

    There's only one button on the page, nothing else ! I need to click twice the button before I can see testing ...
    (it's with a postback)

    I really don't know anymore how I can solve this !

    Thanks in advance,

    Bjorn

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Make sure that the following are true:

    In your @Page directive, autoeventwireup is false and that also, your button is autopostback=true.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    One more thing:

    Page load ALWAYS executes before any event handlers do. Therefore, if you are having a problem because you do not see your message onscreen until the second click, it's not a problem with clicking twice. It's because you are response.writing before you set the variable.

    Page Loads first time:

    Page Load executes, no session exists, blank output

    Button is clicked:

    Page Load executes, no session exists, blank output
    Button Event Handler executes, session created, but not outputted since the PageLoad already executed before this step.

    Button clicked again:

    Page Load executes. Session exists this time, is outputted.
    Button Event Handler executes again, session variable recreated.


    You should make an ASP:Label and in both, the page load and the button event handler, change the .text property to reflect the value of that session variable.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99
    Hello Lord !

    Thanks a lot ! Right now I understand why it didn't execute correctly and I also know a workaround now !

    Thanks !

    Originally posted by Lord_Rat
    One more thing:

    Page load ALWAYS executes before any event handlers do. Therefore, if you are having a problem because you do not see your message onscreen until the second click, it's not a problem with clicking twice. It's because you are response.writing before you set the variable.

    Page Loads first time:

    Page Load executes, no session exists, blank output

    Button is clicked:

    Page Load executes, no session exists, blank output
    Button Event Handler executes, session created, but not outputted since the PageLoad already executed before this step.

    Button clicked again:

    Page Load executes. Session exists this time, is outputted.
    Button Event Handler executes again, session variable recreated.


    You should make an ASP:Label and in both, the page load and the button event handler, change the .text property to reflect the value of that session variable.

  5. #5
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    That is funny. I was having the exact same problem with a part of my code. The funny thing is, I knew the order of events, and I still messed it up...lol. Thanks for the refresh...I needed it.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Belgium
    Posts
    99
    I knew them too but I couldn't think about that first click that first the page loads and then you execute the click event....

    But whatever ... I'm 26 and still got a long way to go ! (If Microsoft doesn't go tooooo fast )


    Originally posted by hellswraith
    That is funny. I was having the exact same problem with a part of my code. The funny thing is, I knew the order of events, and I still messed it up...lol. Thanks for the refresh...I needed it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width