Create dynamic buttons with events

S

StefVan

Hi

I'm building dynamic Web Control buttons. Each button must handle the same event (Click or Command) but only the last created button will reply on the click event and not the others. What's wrong

Many Thanks

Public DomainPlace As PlaceHolde
Public WithEvents CmdDomain As Butto
Sub Page_Load(ByVal s As Object, ByVal e As EventArgs
For Each ud In usr.UserDomain
CmdDomain = New Button(
CmdDomain.Text = ud.DomainDesc
CmdDomain.CommandName = ud.DomainI
CmdDomain.CommandArgument = ud.DomainUr
DomainPlace.Controls.Add(CmdDomain
Nex
End Su

Sub CmdDomainClick(ByVal s As Object, ByVal e As EventArgs) Handles CmdDomain.Clic
' Do something here..
End Sub
 
K

Ken Cox [Microsoft MVP]

Hello StefVan,

You probably want to use AddHandler to give each button an event handler.
Don't restrict the handler code to just one event.

Here's some code to demonstrate:

Public CmdDomain As Button
Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim intCounter As Integer
Dim es As System.EventArgs
For intCounter = 0 To 5
CmdDomain = New Button
CmdDomain.Text = "Button" & intCounter.ToString
CmdDomain.CommandName = "Commandname" & intCounter.ToString
CmdDomain.CommandArgument = "Domain" & intCounter.ToString
AddHandler CmdDomain.Click, AddressOf CmdDomainClick
DomainPlace.Controls.Add(CmdDomain)
Next
End Sub
Sub CmdDomainClick(ByVal s As Object, ByVal e As EventArgs)
Dim btnS As Button
btnS = s
Response.Write(btnS.Text)
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
S

StefVan

Hi Ken

Thanks a lot for your help. It's now working well with the addHandler

Stef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top