Click here to Skip to main content
15,905,776 members
Please Sign up or sign in to vote.
1.33/5 (2 votes)
See more:
Hi,

In the code behind I've created a link button dynamically on page load and provided a event handler as below. But when I clicked the link button it is not using the event handler.
VB
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
dim linkbtn as new LinkButton
With linkbtn
  .Text = "Click Here"
  .Font.Underline = False
  AddHandler linkbtn.Click, AddressOf LinkBtn_Click
End With
PlaceHolder1.Controls.Add(linkbtn)
End Sub

VB
Protected Sub LinkBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  MsgBox("Button is clicked")       
End Sub
Posted
Updated 12-Jul-12 19:40pm
v4
Comments
Sanjay Kunjam 13-Jul-12 1:24am    
What error you are getting??

Hi,
As I think after adding the control to the place-holder you should add handler. Like:

VB
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
dim linkbtn as new LinkButton
With lnkbtn
  .Text = "Click Here"
  .Font.Underline = False
End With
PlaceHolder1.Controls.Add(linkbtn)
AddHandler lnkbtn.Click, AddressOf LinkBtn_Click
End Sub

VB
Protected Sub LinkBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  MsgBox("Button is clicked")
End Sub


This is woriking for me and hope this will help you also.
All the best.
--Amit
 
Share this answer
 
Below is working for me. You had a couple variable names wrong in code above. Also, I wouldn't recommend using MsgBox in server side code of web page.

VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       Dim lnkbtn As New LinkButton
       With lnkbtn
           .Text = "Click Here"
           .Font.Underline = False
           AddHandler lnkbtn.Click, AddressOf LinkBtn_Click
       End With
       PlaceHolder1.Controls.Add(lnkbtn)


   End Sub

   Protected Sub LinkBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
       Response.Write("Link Button Clicked")
   End Sub
 
Share this answer
 
The problem was in declaring ID for the link button. That's why it was not working.

I've converted the time into string and specified it as ID for the link button.

Sorry & Thanks for spending time for me to solve the issue.

Ganesh
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900