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

  • Thread starter Thread starter Ryan Ternier
  • Start date Start date
R

Ryan Ternier

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
 
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.
 
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!
************************************************
 
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
 
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!
************************************************
 
Back
Top