Help w/AddHandler (Not Firing Off)

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Help w/AddHandler (Not Firing Off)

    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 Guest

  2. Similar Questions and Discussions

    1. PageIndexChanged Not Firing
      Bingo! that did the trick. Thanks for your help "Elton Wang" wrote: > Check if your datagrid's enableviewstate is false. Set it to true or when...
    2. AddHandler statement
      I created a DataGrid dynamically using a PlaceHolder; to sort the DataGrid I used the AddHandler statement ( AddHandler myDataGrid.SortCommand,...
    3. Problem using AddHandler for dynamically created WebControls
      I am using the AddHandler statement to add a CheckedChanged event handler to a series of RadioButtons that I create in a loop. However, the handler...
    4. Custom web control - Unable to capture events with AddHandler in an ITemplate
      Ok, I know this has been asked a lot. And I see a lot on the topic, but things are a little different here and it's still not quite working for...
    5. Session_End Not Firing
      Hey guys I have a user tracking setup to track users. What it does is once a user hits my site it sends me an email telling me some info and once a...
  3. #2

    Default Re: Help w/AddHandler (Not Firing Off)

    Handle each postback indvidually why you are lumping them together?


    "Jeffrey A. Voigt" <jvelite@earthlink.net> wrote in message
    news:u8Kvl%230XDHA.1384@TK2MSFTNGP10.phx.gbl...
    > 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\) Guest

  4. #3

    Default Re: Help w/AddHandler (Not Firing Off)

    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)" <sql_agentman@hotmail.com> wrote in message
    news:e4dKp63XDHA.652@TK2MSFTNGP10.phx.gbl...
    > Handle each postback indvidually why you are lumping them together?
    >
    >
    > "Jeffrey A. Voigt" <jvelite@earthlink.net> wrote in message
    > news:u8Kvl%230XDHA.1384@TK2MSFTNGP10.phx.gbl...
    > > 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 Guest

  5. #4

    Default Re: Help w/AddHandler (Not Firing Off)

    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 [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur Guest

  6. #5

    Default Re: Help w/AddHandler (Not Firing Off)

    Jeff hi,

    yes, firing off whenever there is a postback caused by those
    controls.

    Try to create new project and run it with the code. Maybe something in
    the application causes the miss behavior.

    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 [url]http://www.developersdex.com[/url] ***
    Don't just participate in USENET...get rewarded for it!
    Natty Gur Guest

  7. #6

    Default Re: Help w/AddHandler (Not Firing Off)

    is your code that set's up the events ran every time there is a post back?

    "Natty Gur" <natty@dao2com.com> wrote in message
    news:emMpMiBYDHA.2152@TK2MSFTNGP09.phx.gbl...
    > Jeff hi,
    >
    > yes, firing off whenever there is a postback caused by those
    > controls.
    >
    > Try to create new project and run it with the code. Maybe something in
    > the application causes the miss behavior.
    >
    > 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 [url]http://www.developersdex.com[/url] ***
    > Don't just participate in USENET...get rewarded for it!

    Jeff Voigt Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139