Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4992
  • Last Modified:

Programmatically add a linkbutton with onclick event

Its easy to write a linkbutton into an aspx page like so:

<asp:LinkButton ID="lbDate" runat="server" Text="Click Me" OnClick="lbDate_click" CommandArgument="1" />

But how do I dynamically create one?  The Onclick method is protected and cannot be set programmatically.  Here's what I'm trying to do...

[using vb]

For i = 0 To 9
  Dim lbDate As LinkButton = New LinkButton
  lbDate.ID = "lb" & i
  lbDate.Text = "Test " & i
  lbDate.Onclick = "lbDate_click"   'Doesn't work, onclick is protected.  HELP!
  lbDate.CommandArgument = i
  placeholder.Controls.Add(lbDate)
Next
0
osubvr
Asked:
osubvr
  • 2
  • 2
1 Solution
 
dharmesh_amityCommented:
lbDate.Click += new System.EventHandler(this.lbDate_click);
0
 
osubvrAuthor Commented:
Sorry, now I feel stupid.

I neglected to mention that I'm using the .NET Framework 2.0beta.

When I use your example, I get the following error:

Compiler Error Message: BC32022: 'Public Event Click(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

Source Error:

Line 137:            lbDate.CommandArgument = DateAdd(DateInterval.Day, -i, Date.Today)
Line 138:            lbDate.Text = DateAdd(DateInterval.Day, -i, Date.Today)
Line 139:            lbDate.Click += New System.EventHandler(this.lbDate_click)

 
0
 
osubvrAuthor Commented:
Oops.  On this server we're still on .net2.0alpha... Microsoft .NET Framework Version:2.0.40607.42

0
 
dharmesh_amityCommented:
I really apologise for this

I got all confused between VB.NET and C#.NET

This is final and correct

AddHandler lbDate.Click, AddressOf Me.lbDate_click
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now