ASP.NET: Dynamic Button Event Handlers

Posted on 2004-11-16
Last Modified: 2008-01-16
Hello experts,

I am working on an ASP.NET project with VB backend, and have run into a slight problem.  We generate buttons dynamically (up/down arrows to reorder the records in the dataset) - but we can't figure out how to assign an event to each dynamically generated button.

Here is how we create the buttons, and our unsuccessful attempt to add a handler...

dim i as integer
for i = 0 to 'number of rows in datatable -1
                'create up/down btns
                Dim btnUp As New Button
                btnUp.ID = "btnUp" & i
                AddHandler btnUp.Click, AddressOf btnUpClick
                btnUp.Text = "UP"

    Private Sub btnUpClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.errorText.Text = "trapped up click"
    End Sub

Thanks in advance for any help...let me know if you need any more info.


Question by:jesselavaca
    LVL 25

    Assisted Solution

    LVL 28

    Assisted Solution

    Hi jesselavaca,
    what is lblPrevius? is it a Label control?
    if yes i would suggest you to use panel control and in there put your buttons
    actually your code looks correct

    LVL 1

    Author Comment

    Thanks, reviewing your responses...

    LVL 8

    Expert Comment

    Long story short, you want to use the AddHandler function to dynamically assign an event handler to the control's event:

    It goes a little something like:

    AddHandler MyObject.Event1, AddressOf Me.MyEventHandler

    Where (as you can see) the first argument is the event, and the second is a reference to the method (conforming to the appropriate delegate) you're using to handle the event.
    LVL 8

    Accepted Solution

    Oops. Should've read your whole post. Sorry.

    When creating dynamic controls, you must recreate them on PostBack at the appropriate point. (I believe you may need to use Page Init() for your purposes).
    LVL 1

    Author Comment

    We had to recreate on postback and it worked...


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now