Help w/AddHandler (Not Firing Off)

Discussion in 'ASP .Net' started by Jeffrey A. Voigt, Aug 10, 2003.

  1. 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 A. Voigt, Aug 10, 2003
    #1
    1. Advertising

  2. Handle each postback indvidually why you are lumping them together?


    "Jeffrey A. Voigt" <> wrote in message
    news:u8Kvl%...
    > 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
    >
    >
    >
    > ********************************************************************
    >
    > ********************************************************************
    >
    > ********************************************************************
    >
    >
     
    MS News \(MS ILM\), Aug 10, 2003
    #2
    1. Advertising

  3. 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)" <> wrote in message
    news:...
    > Handle each postback indvidually why you are lumping them together?
    >
    >
    > "Jeffrey A. Voigt" <> wrote in message
    > news:u8Kvl%...
    > > 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 A. Voigt, Aug 11, 2003
    #3
  4. Jeffrey A. Voigt

    Natty Gur Guest

    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


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Natty Gur, Aug 11, 2003
    #4
    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. Natty Gur

    Re: Help w/AddHandler (Not Firing Off)

    Natty Gur, Aug 11, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    436
    Jeff Voigt
    Aug 11, 2003
  2. Tee
    Replies:
    1
    Views:
    3,302
    Martin Dechev
    Feb 18, 2004
  3. RA
    Replies:
    2
    Views:
    2,935
  4. =?Utf-8?B?Sm9obiBBdXN0aW4=?=

    Please explain why AddHandler works in Page_Load, but not Button_C

    =?Utf-8?B?Sm9obiBBdXN0aW4=?=, Sep 26, 2006, in forum: ASP .Net
    Replies:
    8
    Views:
    514
    =?Utf-8?B?Sm9obiBBdXN0aW4=?=
    Oct 2, 2006
  5. Nathan Sokalski

    AddHandler not working

    Nathan Sokalski, Oct 2, 2007, in forum: ASP .Net
    Replies:
    15
    Views:
    860
    Cor Ligthert [MVP]
    Oct 3, 2007
Loading...

Share This Page