ASP.NET: Dynamic Button Event Handlers

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
                Me.lblPrevious.Controls.Add(btnUp)
                btnUp.Text = "UP"
next

    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.

-Jess

LVL 1
jesselavacaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mmarinovCommented:
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

Regards!
B..M
mmarinov
0
jesselavacaAuthor Commented:
Thanks, reviewing your responses...

-Jess
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconaddonremoveon.asp

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.
0
boulder_bumCommented:
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).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jesselavacaAuthor Commented:
We had to recreate on postback and it worked...

-Jess
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.