Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1975
  • Last Modified:

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

0
jesselavaca
Asked:
jesselavaca
3 Solutions
 
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
jesselavacaAuthor Commented:
We had to recreate on postback and it worked...

-Jess
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now