How do I create an event for a dynamically created button?

I have a button that is loaded onto the page dynamically (with another button click actually). I assign it an ID, etc in the code behind file, but since it isn't actually created until after the page loads, I cant assign it a click event:

Compiler Error Message: BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

Source Error:

Line 169:    End Sub
Line 170:
Line 171: Protected Sub erase_0_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles erase_0.Click
Line 172: dump.Text = "HEY!"
Line 173:

 How do I attach an event to this button? If the compiler would just ignore the fact that I haven't created this control yet and run the application it would work fine.


here is some more of my code:

    Protected Sub AddMember_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddMember.Click

        If formCounter < 6 Then
            formCounter += 1
            Dim namebx As New TextBox
            Dim dobbx As New TextBox
            Dim genderbx As New DropDownList
            Dim statusbx As New DropDownList
            Dim ssnbx As New TextBox
            Dim countlbl As New Label
            Dim erasebtn As New Button 'This is the new button

            namebx.ID = "namebx_" & formCounter
            dobbx.ID = "dobbx_" & formCounter
            genderbx.ID = "genderbx_" & formCounter
            statusbx.ID = "statusbx_" & formCounter
            ssnbx.ID = "ssnbx_" & formCounter
            ssnbx.Width = 90
            dobbx.Width = 80
            namebx.Width = 100
            statusbx.Width = 80
            erasebtn.Text = "X"
            erasebtn.ToolTip = "Remove"
            erasebtn.ID = "erase_" & formCounter     'assigning it an ID (erase_0 first time)
            erasebtn.Font.Bold = True
            countlbl.Text = " " & (formCounter + 1) & ". "
            namebx.Font.Bold = True
            dobbx.Font.Bold = True
            genderbx.Font.Bold = True
            statusbx.Font.Bold = True
            ssnbx.Font.Bold = True
            countlbl.Font.Bold = True

            PlaceHolder1.Controls.Add(GetLiteral("<td colspan='5'>"))
            PlaceHolder1.Controls.Add(GetLiteral("</tr>")) 'Inserting everything into a placeholder

            ERROR_MESSAGES.InnerHtml = "Up to 7 Allowed"
        End If
    End Sub


Of course my click even is just a simple test function to see if erase_0 will work:

Protected Sub erase_0_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles erase_0.Click
        dump.Text = "HEY!"
    End Sub


Basically I need to assign a method to each instance of this erase button that I can call when it is clicked.

keepin' it real in the ATX.
3/28/2007 8:13:43 PM 93655 articles. 5 followers. Follow

1 Replies

Similar Articles

[PageSpeed] 36

Use the AddHandler statement to add an event handler to dynamically created button.  Example: 
' Create the button
dim btn as New Button()
with btn
   .ID = "SomeID"
   .Text = "Check Me"
   ' Associate an event handler with an event.
    AddHandler btn.Click, AddressOf DynamicBtnClick
end with

protected sub DynamicBtnClick(sender,e)
  ' Do something here
end sub
3/28/2007 9:18:55 PM

Web resources about - How do I create an event for a dynamically created button? -

Placebase team at Apple file “Schematic Maps” patent dynamically detailing important data
In a report from Computerworld way back in 2009, we learned that Apple had quietly acquired Maps API company Placebase. Then, founder of Placebase ...

Apple's Lightning port dynamically assigns pins to allow for reversible use
... a closer look at how Apple's new Lightning connector works, and has come to the conclusion that the 8 pins on each side of the plug are dynamically ...

In a dynamically typed language, is it a bad idea to return different data types?
In some cases, returning different types from a single function works.

Urlbox Launches Screenshots-as-a-Service Platform to Dynamically Update Galleries
... changes. Launched yesterday, Urlbox allows anyone with a gallery displaying website screenshots to automate the process and keep them dynamically ... launcher dynamically adapts to what you’re doing right now
... phone according to what you’re doing. Perform a search for ‘football’ and your background will change to the NFL logo. A launcher that dynamically ...

A YouTube Experiment Resizes the Player Dynamically
There's a new YouTube experiment that changes the video player's size, depending on the browser window's size. When resizing Chrome's window, ...

Apple Granted “Dynamically Changing Cursor for User Interface” Patent, Originally Filed in 2003
... a utility patent that covers the use of the mouse cursor that changes according to the context of the task it is engaged in. Called a “Dynamically ...

Atmel's FPSLIC II Dynamically Reconfigurable SoC Supports "Silicon-Sharing" For Peripherals & Interfaces ...
Silicon-Sharing Fits 200K Gate Design into 40K Gate FPSLIC II's, Cuts Power Drain by 97% Launcher Wants to Dynamically Change Phones Based on Location, Interests, and Mental State ...
What if your phone’s launcher dynamically changed based upon interests, your location, or topics you had in mind at a specific moment? A new ...

Resources last updated: 1/14/2016 2:18:04 PM