Server Side button calling page_load before calling it's own click event.

Discussion in 'ASP .Net' started by Ryan Ternier, Jul 28, 2004.

  1. Ryan Ternier

    Ryan Ternier Guest

    I have a button event:


    Public Sub SwitchItem(ByVal sender As System.Object, ByVal e As
    System.EventArgs)
    Dim btnTest As New Button
    Dim astrTest As String()
    btnTest = CType(sender, Button)
    astrTest = btnTest.ClientID.Split("_")
    strControlsToEdit = astrTest(2)
    'Find out what button was pressed, then pass the flags to the
    TraverseControls method.
    If btnTest.Text.ToLower = "allow" Then
    TraverseControls(Page, False, True)
    ElseIf btnTest.Text.ToLower = "disallow" Then
    TraverseControls(Page, False, False)
    End If
    End Sub
     
    Ryan Ternier, Jul 28, 2004
    #1
    1. Advertising

  2. Ryan Ternier

    Ryan Ternier Guest

    Sorry, It sent without the previous being finished.
    I have a function:

    Public Sub SwitchItem(ByVal sender As System.Object, ByVal e As
    System.EventArgs)
    Dim btnTest As New Button
    Dim astrTest As String()
    btnTest = CType(sender, Button)
    astrTest = btnTest.ClientID.Split("_")
    strControlsToEdit = astrTest(2)
    'Find out what button was pressed, then pass the flags to the
    TraverseControls method.
    If btnTest.Text.ToLower = "allow" Then
    TraverseControls(Page, False, True)
    ElseIf btnTest.Text.ToLower = "disallow" Then
    TraverseControls(Page, False, False)
    End If
    End Sub
    ___________________________
    That is called By:

    <asp:button cssclass="BoxType42" value="Disallow" id="btnDisAllow"
    Text="Disallow" name="Disallow"
    onclick="SwitchItem" runat="server" />

    After the page loads up, and a user clicks this button, it will trigger the
    page load event, reload the page, and not call the button's function.
    However, if you click it again, it will call it. Why is this, and is there a
    way around it?

    The Button is bound inside a repeater, that's why I coded the onclick event.
     
    Ryan Ternier, Jul 28, 2004
    #2
    1. Advertising

  3. That is normal behavior, as Page_Load is called when the page is loaded,
    prior to checking why the page is being loaded. It is the reason most coders
    have:

    If (Page.IsPostBack = False) Then
    'run load code
    Else
    'run postback code
    'I hate this part of the IF myself, at least as I most
    'commonly see it implemented
    End If



    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    ************************************************
    Think Outside the Box!
    ************************************************
    "Ryan Ternier" <> wrote in message
    news:...
    > I have a button event:
    >
    >
    > Public Sub SwitchItem(ByVal sender As System.Object, ByVal e As
    > System.EventArgs)
    > Dim btnTest As New Button
    > Dim astrTest As String()
    > btnTest = CType(sender, Button)
    > astrTest = btnTest.ClientID.Split("_")
    > strControlsToEdit = astrTest(2)
    > 'Find out what button was pressed, then pass the flags to the
    > TraverseControls method.
    > If btnTest.Text.ToLower = "allow" Then
    > TraverseControls(Page, False, True)
    > ElseIf btnTest.Text.ToLower = "disallow" Then
    > TraverseControls(Page, False, False)
    > End If
    > End Sub
    >
    >
     
    Cowboy \(Gregory A. Beamer\) [MVP], Jul 28, 2004
    #3
  4. Ryan Ternier

    Ryan Ternier Guest

    Yes, I have that implemented right now, but it still seems to want to reload
    the page on that button click, without fireing the click event.

    This is how I currently have it:

    If Not IsPostBack Then
    'On the first load
    Page.RegisterStartupScript("MyScriptKey",
    "<script>parent.frames.IFrameAdvance.resizeTo
    (760,500);</script>")
    ....more l33t code.
    Else
    Dim test As String
    test = "WHY ARE YOU GOING HERE J00 m34n c0mp1l3r"
    End If

    I really do nothing in the else, but always have a breakpoint set there
    incase it hits it, so I can keep track on what
    ..NET is doing for performance reasons.

    I'll see if I can work something out like a reload or refresh of the page
    and see if that can stop the first click fiasco.

    /RT


    "Cowboy (Gregory A. Beamer) [MVP]" <> wrote
    in message news:...
    > That is normal behavior, as Page_Load is called when the page is loaded,
    > prior to checking why the page is being loaded. It is the reason most

    coders
    > have:
    >
    > If (Page.IsPostBack = False) Then
    > 'run load code
    > Else
    > 'run postback code
    > 'I hate this part of the IF myself, at least as I most
    > 'commonly see it implemented
    > End If
    >
    >
    >
    > --
    > Gregory A. Beamer
    > MVP; MCP: +I, SE, SD, DBA
    >
    > ************************************************
    > Think Outside the Box!
    > ************************************************
    > "Ryan Ternier" <> wrote in message
    > news:...
    > > I have a button event:
    > >
    > >
    > > Public Sub SwitchItem(ByVal sender As System.Object, ByVal e As
    > > System.EventArgs)
    > > Dim btnTest As New Button
    > > Dim astrTest As String()
    > > btnTest = CType(sender, Button)
    > > astrTest = btnTest.ClientID.Split("_")
    > > strControlsToEdit = astrTest(2)
    > > 'Find out what button was pressed, then pass the flags to the
    > > TraverseControls method.
    > > If btnTest.Text.ToLower = "allow" Then
    > > TraverseControls(Page, False, True)
    > > ElseIf btnTest.Text.ToLower = "disallow" Then
    > > TraverseControls(Page, False, False)
    > > End If
    > > End Sub
    > >
    > >

    >
    >
     
    Ryan Ternier, Jul 28, 2004
    #4
  5. The Page_Load will always fire, so that has to be taken out of the equation.
    If the Click event is not firing, see if the "Handles XXX.Change" (or
    whatever) is missing. Sometimes the events get out of sync. To resync, you
    can either type this in or you can reset the event up (normally double
    clicking on the control in question).

    I still have not found the unique circumstances that cause this to happen,
    but the VS IDE messes up event handlers from time to time.

    If that is not it, post a follow up.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA

    ************************************************
    Think Outside the Box!
    ************************************************
    "Ryan Ternier" <> wrote in message
    news:%...
    > Yes, I have that implemented right now, but it still seems to want to

    reload
    > the page on that button click, without fireing the click event.
    >
    > This is how I currently have it:
    >
    > If Not IsPostBack Then
    > 'On the first load
    > Page.RegisterStartupScript("MyScriptKey",
    > "<script>parent.frames.IFrameAdvance.resizeTo
    > (760,500);</script>")
    > ....more l33t code.
    > Else
    > Dim test As String
    > test = "WHY ARE YOU GOING HERE J00 m34n c0mp1l3r"
    > End If
    >
    > I really do nothing in the else, but always have a breakpoint set there
    > incase it hits it, so I can keep track on what
    > .NET is doing for performance reasons.
    >
    > I'll see if I can work something out like a reload or refresh of the page
    > and see if that can stop the first click fiasco.
    >
    > /RT
    >
    >
    > "Cowboy (Gregory A. Beamer) [MVP]" <>

    wrote
    > in message news:...
    > > That is normal behavior, as Page_Load is called when the page is loaded,
    > > prior to checking why the page is being loaded. It is the reason most

    > coders
    > > have:
    > >
    > > If (Page.IsPostBack = False) Then
    > > 'run load code
    > > Else
    > > 'run postback code
    > > 'I hate this part of the IF myself, at least as I most
    > > 'commonly see it implemented
    > > End If
    > >
    > >
    > >
    > > --
    > > Gregory A. Beamer
    > > MVP; MCP: +I, SE, SD, DBA
    > >
    > > ************************************************
    > > Think Outside the Box!
    > > ************************************************
    > > "Ryan Ternier" <> wrote in message
    > > news:...
    > > > I have a button event:
    > > >
    > > >
    > > > Public Sub SwitchItem(ByVal sender As System.Object, ByVal e As
    > > > System.EventArgs)
    > > > Dim btnTest As New Button
    > > > Dim astrTest As String()
    > > > btnTest = CType(sender, Button)
    > > > astrTest = btnTest.ClientID.Split("_")
    > > > strControlsToEdit = astrTest(2)
    > > > 'Find out what button was pressed, then pass the flags to the
    > > > TraverseControls method.
    > > > If btnTest.Text.ToLower = "allow" Then
    > > > TraverseControls(Page, False, True)
    > > > ElseIf btnTest.Text.ToLower = "disallow" Then
    > > > TraverseControls(Page, False, False)
    > > > End If
    > > > End Sub
    > > >
    > > >

    > >
    > >

    >
    >
     
    Cowboy \(Gregory A. Beamer\) [MVP], Jul 29, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    843
    Purvi T
    Oct 19, 2004
  2. jorge
    Replies:
    2
    Views:
    1,271
    jorge
    May 26, 2004
  3. =?Utf-8?B?Sm9zZVZhbGVuY2lh?=

    How to avoid execution of page_load event before click event of a

    =?Utf-8?B?Sm9zZVZhbGVuY2lh?=, Apr 3, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    615
    =?Utf-8?B?Sm9zZVZhbGVuY2lh?=
    Apr 4, 2006
  4. jorge
    Replies:
    1
    Views:
    414
    Teemu Keiski
    May 28, 2004
  5. jorge
    Replies:
    0
    Views:
    329
    jorge
    May 25, 2004
Loading...

Share This Page