osubvr
asked on
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(l bDate)
Next
<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(l
Next
lbDate.Click += new System.EventHandler(this.l bDate_clic k);
ASKER
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.l bDate_clic k)
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.l
ASKER
Oops. On this server we're still on .net2.0alpha... Microsoft .NET Framework Version:2.0.40607.42
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.