Control added at runtime not raising events.

W

Wade

Hi all,

I am dynamically adding a link button to a page, but for some reason it is
not raising the click event in the code behind. Here are some snippets from
my code -- any help is greatly appreciated:

' declaring variable
Protected WithEvents lbtEquipment As
System.Web.UI.WebControls.LinkButton

...

' setting properties
lbtEquipment = New System.Web.UI.WebControls.LinkButton
lbtEquipment.Text = "New"
lbtEquipment.ID = "lbtEquipment"
lbtEquipment.CssClass = "GridDataEntryNew"

' adding control to table cell
tcNew.Controls.Add(lbtFreeboard)

...

' trying to handle the click event
Private Sub lbtEquipment_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles lbtEquipment.Click
Try
Response.Write("test")

Catch ex As Exception
End Try
End Sub

I know this is a bit out of context, but hopefully someone can tell me what
I'm missing. I want the linkbutton, which was added to the table cell, to
raise the lbtEquipment_Click event when it's clicked!

Thanks!

Wade
 
K

Karl Seguin

You need to make sure lbtEquipment is created on postback and that this
happens before the certain events (LoadViewStateSecondTry I think is the
event in question)...this typically means the control must be recreated on
Page_Load.

My guess is that you aren't doing one of those two things...

Karl
 
W

Wade

Hi Karl,

It's extremely possible that this is the case. So, I have made a number of
modifications, but I still cannot get the event to raise. Here is what I'm
doing.

' declaring the control
Protected WithEvents lbtEquipment As
System.Web.UI.WebControls.LinkButton

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

' creating the control
lbtEquipment = New System.Web.UI.WebControls.LinkButton

lbtEquipment.Text = "New"
lbtEquipment.ID = "lbtEquipment"

lbtEquipment.CssClass = "GridDataEntryNew"

' Add event handler
AddHandler lbtEquipment.Click, AddressOf lbtEquipment_Click

End Sub

....

' The control is added to the page later
tcNew.Controls.Add(lbtEquipment)

....

Private Sub lbtEquipment_Click(ByVal sender As Object, ByVal e As
System.EventArgs)
Response.Write("equipment")
End Sub


I am still unable to raise the lbtEquipment_Click event. Any ideas?
 
W

Wade

Hi guys,

Okay -- I figured out what's going on. Turns out that I have been adding it
to tcNew, which is a tablecell. Unfortunately, tcNew itself was dynamically
created, and it was not persisting in the postback. Consequently, the link
button was not even in the page as a result of tcNew not existing.

I figured this out by creating a placeholder and adding my link button to
the place holder. Once I did this, I was able to get the click event for
the link button to run perfectly.

Thanks for all your guys help!

Wade
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top