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
********************************************************************
********************************************************************
********************************************************************
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
********************************************************************
********************************************************************
********************************************************************