Help with Raising Events for Dynamically Created Controls

webappguru

Member
Joined
Jan 16, 2005
Messages
7
Programming Experience
3-5
Hey Guys,

I was all excited that I can programmatically create command buttons by

dim newbutton as button
controls.add(newbutton)

Now! How do I get code behind a command button that was dynamically created?

Thanks

Ali
 

Paszt

Staff member
Joined
Jun 3, 2004
Messages
1,500
Location
Raleigh, NC - USA
Programming Experience
Beginner
create a procedure for the event that you want handled by the dynamically created button (it must have the same signature as the event you want handled), then use an addhandler statement.

VB.NET:
Private Sub SomeProcedure(ByVal sender As Object, _
  ByVal e As EventArgs)
    'code for newbutton goes here
End Sub

Dim newbutton As Button
Controls.Add(newbutton)
AddHandler newbutton.Click, AddressOf SomeProcedure
 
Top Bottom