Help w/AddHandler (Not Firing Off)

  • Thread starter Thread starter Jeffrey A. Voigt
  • Start date Start date
J

Jeffrey A. Voigt

Can someone take a quick glace at my code and tell me why my
AutoPostBackHandler function does not get fired off at all? What I'm trying
to do is get all of the Buttons and DropDownList controls that have an
AutoPostBack property set to true to fire off the AutoPostBackHandler
function dynamically. I achive this by going through all controls that are
on the WebPage and seting up the handler on the fly. I've debugged this and
know for a fact that the AddHandler's are in fact being called with no
errors. The problem is though when the user clicks on a button and/or
change the index of the dropdown (and when page is posted back to the
server) The AutoPostBackHandler is NOT being triggered.

Is there something I'm doing wrong?

Thanks,
- Jeff

********************************************************************

********************************************************************

********************************************************************



Imports System.Text

Imports System.Reflection



Public Class BaseWebPage

Inherits Page



Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' if this is a post back then get out

If Page.IsPostBack Then Return



' go through all controls and setup an auto post back handler

' for all controls that have their auto post back property set to
true

RegisterAutoPostBackHandlers(Page)

End Sub



Private Sub RegisterAutoPostBackHandlers(ByRef c As Control)

' get the type name of the control

Dim typeName As String = c.GetType().Name



' only register autopost back event handlers for the following
controls

Select Case typeName



Case "Button"

' cast to button

Dim b As Button = CType(c, Button)

' buttons always post back to add the handler

AddHandler b.Click, AddressOf AutoPostBackHandler



Case "DropDownList"

' cast to a drop down list

Dim ddl As DropDownList = CType(c, DropDownList)

' register the handler if the autopostback is set to true

If ddl.AutoPostBack Then

AddHandler ddl.SelectedIndexChanged, AddressOf
AutoPostBackHandler

End If



End Select



' return if the control has no children

If Not c.HasControls Then Return



' recurse through children

Dim i As Integer

For i = 0 To c.Controls.Count - 1

RegisterAutoPostBackHandlers(c.Controls(i))

Next

End Sub



' handler for all controls that just auto posted back

Public Sub AutoPostBackHandler(ByVal sender As Object, ByVal e As
System.EventArgs)

' register startup script to navigate back to the button

RegisterStartupScript("ScrollBack", "<script>document.forms[0]." +
sender.GetType().Name + ".ScrollTo();</script>")

End Sub



End Class



********************************************************************

********************************************************************

********************************************************************
 
Handle each postback indvidually why you are lumping them together?


Jeffrey A. Voigt said:
Can someone take a quick glace at my code and tell me why my
AutoPostBackHandler function does not get fired off at all? What I'm trying
to do is get all of the Buttons and DropDownList controls that have an
AutoPostBack property set to true to fire off the AutoPostBackHandler
function dynamically. I achive this by going through all controls that are
on the WebPage and seting up the handler on the fly. I've debugged this and
know for a fact that the AddHandler's are in fact being called with no
errors. The problem is though when the user clicks on a button and/or
change the index of the dropdown (and when page is posted back to the
server) The AutoPostBackHandler is NOT being triggered.

Is there something I'm doing wrong?

Thanks,
- Jeff

********************************************************************

********************************************************************

********************************************************************



Imports System.Text

Imports System.Reflection



Public Class BaseWebPage

Inherits Page



Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' if this is a post back then get out

If Page.IsPostBack Then Return



' go through all controls and setup an auto post back handler

' for all controls that have their auto post back property set to
true

RegisterAutoPostBackHandlers(Page)

End Sub



Private Sub RegisterAutoPostBackHandlers(ByRef c As Control)

' get the type name of the control

Dim typeName As String = c.GetType().Name



' only register autopost back event handlers for the following
controls

Select Case typeName



Case "Button"

' cast to button

Dim b As Button = CType(c, Button)

' buttons always post back to add the handler

AddHandler b.Click, AddressOf AutoPostBackHandler



Case "DropDownList"

' cast to a drop down list

Dim ddl As DropDownList = CType(c, DropDownList)

' register the handler if the autopostback is set to true

If ddl.AutoPostBack Then

AddHandler ddl.SelectedIndexChanged, AddressOf
AutoPostBackHandler

End If



End Select



' return if the control has no children

If Not c.HasControls Then Return



' recurse through children

Dim i As Integer

For i = 0 To c.Controls.Count - 1

RegisterAutoPostBackHandlers(c.Controls(i))

Next

End Sub



' handler for all controls that just auto posted back

Public Sub AutoPostBackHandler(ByVal sender As Object, ByVal e As
System.EventArgs)

' register startup script to navigate back to the button

RegisterStartupScript("ScrollBack", "<script>document.forms[0]." +
sender.GetType().Name + ".ScrollTo();</script>")

End Sub



End Class



********************************************************************

********************************************************************

********************************************************************
 
I do handle each post back seperately, but i'm also creating a dynamic
handler for some auto scrolling on each postback. I'm doing this in a base
class so that all webpages get this effect. Now can someone help me answer
my initial question instead of asking one instead? :)

Thanks,
- Jeff

MS News (MS ILM) said:
Handle each postback indvidually why you are lumping them together?


Jeffrey A. Voigt said:
Can someone take a quick glace at my code and tell me why my
AutoPostBackHandler function does not get fired off at all? What I'm trying
to do is get all of the Buttons and DropDownList controls that have an
AutoPostBack property set to true to fire off the AutoPostBackHandler
function dynamically. I achive this by going through all controls that are
on the WebPage and seting up the handler on the fly. I've debugged this and
know for a fact that the AddHandler's are in fact being called with no
errors. The problem is though when the user clicks on a button and/or
change the index of the dropdown (and when page is posted back to the
server) The AutoPostBackHandler is NOT being triggered.

Is there something I'm doing wrong?

Thanks,
- Jeff

********************************************************************

********************************************************************

********************************************************************



Imports System.Text

Imports System.Reflection



Public Class BaseWebPage

Inherits Page



Private Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' if this is a post back then get out

If Page.IsPostBack Then Return



' go through all controls and setup an auto post back handler

' for all controls that have their auto post back property set to
true

RegisterAutoPostBackHandlers(Page)

End Sub



Private Sub RegisterAutoPostBackHandlers(ByRef c As Control)

' get the type name of the control

Dim typeName As String = c.GetType().Name



' only register autopost back event handlers for the following
controls

Select Case typeName



Case "Button"

' cast to button

Dim b As Button = CType(c, Button)

' buttons always post back to add the handler

AddHandler b.Click, AddressOf AutoPostBackHandler



Case "DropDownList"

' cast to a drop down list

Dim ddl As DropDownList = CType(c, DropDownList)

' register the handler if the autopostback is set to true

If ddl.AutoPostBack Then

AddHandler ddl.SelectedIndexChanged, AddressOf
AutoPostBackHandler

End If



End Select



' return if the control has no children

If Not c.HasControls Then Return



' recurse through children

Dim i As Integer

For i = 0 To c.Controls.Count - 1

RegisterAutoPostBackHandlers(c.Controls(i))

Next

End Sub



' handler for all controls that just auto posted back

Public Sub AutoPostBackHandler(ByVal sender As Object, ByVal e As
System.EventArgs)

' register startup script to navigate back to the button

RegisterStartupScript("ScrollBack", "<script>document.forms[0]." +
sender.GetType().Name + ".ScrollTo();</script>")

End Sub



End Class



********************************************************************

********************************************************************

********************************************************************
 
Jeffrey Hi,

I dont see any problem. I run this code which is basicly yours and it
works.

Private Sub ddo(ByRef tar As Control)
Dim control As Control
For Each control In tar.Controls
Select Case control.GetType().Name
Case "Button"
AddHandler CType(control, Button).Click, AddressOf
Natty
Case "ListBox"
AddHandler CType(control,
ListBox).SelectedIndexChanged, AddressOf Natty

End Select
If tar.Controls.Count > 0 Then
ddo(control)
End If
Next

End Sub

Natty Gur, CTO
Dao2Com Ltd.
34th Elkalay st. Raanana
Israel , 43000
Phone Numbers:
Office: +972-(0)9-7740261
Fax: +972-(0)9-7740261
Mobile: +972-(0)58-888377
 
Back
Top