Thursday, March 29, 2012

Setting OnCommand when adding LinkButton dynamically

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,
SherylHi 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

"Sheryl Landon" <shland@.comcast.net> wrote in message
news:%23q9AXsjOFHA.624@.TK2MSFTNGP10.phx.gbl...
> 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

0 comments:

Post a Comment