Setting OnCommand when adding LinkButton dynamically

S

Sheryl Landon

Is there a way to set the OnCommand or OnClick attributes when adding a
LinkButton dynamically through code? These attributes are available when
adding on design-time...

Thanks,
Sheryl
 
K

Ken Cox [Microsoft MVP]

Hi Sheryl,

You can use the AddHandler statement to add any of a control's available
events. When you do that, you need to provide the subroutine that runs when
the event fires. The subroutine has to accept the same parameters as the
event passes. Here's some code to show the idea:

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim lnkbtn As New LinkButton
lnkbtn.Text = "My Link"
lnkbtn.CommandArgument = "MyArg"
AddHandler lnkbtn.Click, AddressOf ClickHandler
PlaceHolder1.Controls.Add(lnkbtn)
End Sub

Sub ClickHandler _
(ByVal s As Object, ByVal e As EventArgs)
Response.Write("Clicked")
End Sub

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]
Toronto
 

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,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top