Dynamically added command buttons

  • Thread starter Geoff Pennington
  • Start date
G

Geoff Pennington

I need to create a table with a moderately complex layout - so a grid
control won't quite do it. Some of the rows need command buttons. I don't
know until run time how many rows I have or which ones need the buttons.

I can add the buttons easily enough with the code

bt = New Button()

bt.Text = "Select"

bt.CommandName = "Select" & strHour

bt.CommandArgument = strHour24

sc.Controls.Add(bt)

but there doesn't seem to be a way to specify an onclick/onCommand argument,
so I can respond to the click. Suggestions?

Much obliged.
 
K

Ken Cox [Microsoft MVP]

Hi Geoff,

Not sure that this is what you need, but I suspect that you're looking for
AddHandler to dynamically add support for the click event of each button.

The code below creates buttons dynamically, adds the CommandName as it is built
and finally fetches it after the click.

Does this help?

Ken
MVP [ASP.NET]



Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim intCounter As Integer
Dim bt As Button
Dim strHour As String
For intCounter = 1 To 5
strHour = intCounter.ToString
bt = New Button
bt.Text = "Select"
bt.CommandName = "Select" & strHour
bt.CommandArgument = strHour
sc.Controls.Add(bt)
AddHandler bt.Click, AddressOf clickhandle
Next

End Sub

Public Sub clickhandle _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim btn As Button
btn = CType(sender, Button)
Label1.Text = btn.CommandName
End Sub


--
Microsoft MVPs have a question for *you*: Are you patched against the Worm?
http://www.microsoft.com/security/security_bulletins/ms03-026.asp



I need to create a table with a moderately complex layout - so a grid
control won't quite do it. Some of the rows need command buttons. I don't
know until run time how many rows I have or which ones need the buttons.

I can add the buttons easily enough with the code

bt = New Button()

bt.Text = "Select"

bt.CommandName = "Select" & strHour

bt.CommandArgument = strHour24

sc.Controls.Add(bt)

but there doesn't seem to be a way to specify an onclick/onCommand argument,
so I can respond to the click. Suggestions?

Much obliged.
 

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

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top