Setting OnCommand when adding LinkButton dynamically

  • Thread starter Thread starter Sheryl Landon
  • Start date Start date
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
 
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
 
Back
Top