M
MS Newsgroups
Hi,
I have a scenario where I am dynamically adding a control from code when a
controls event is fired. The problem I have is that when the newly created
control is clicked, the click event does not fire. I have written a simple
demonstration that reproduces the problem and would appreciate if someone
could give me a hint on how this should be done
Many thanks
Niclas
My code:
Public Class WebForm1
Inherits System.Web.UI.Page
Friend WithEvents button1 As Button
Protected WithEvents pl2 As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents pl1 As System.Web.UI.WebControls.PlaceHolder
Friend WithEvents button2 As Button
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
button1 = New Button
button1.Text = "First Button"
pl1.Controls.Add(button1)
' Add the first button when the page is loaded
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles button1.Click
button2 = New Button
button2.Text = "Second Button"
pl2.Controls.Add(button2)
'add another button when the first button is clicked
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles button2.Click
Response.Redirect("http://www.microsoft.com")
End Sub
'This event is never fired
End Class
I have a scenario where I am dynamically adding a control from code when a
controls event is fired. The problem I have is that when the newly created
control is clicked, the click event does not fire. I have written a simple
demonstration that reproduces the problem and would appreciate if someone
could give me a hint on how this should be done
Many thanks
Niclas
My code:
Public Class WebForm1
Inherits System.Web.UI.Page
Friend WithEvents button1 As Button
Protected WithEvents pl2 As System.Web.UI.WebControls.PlaceHolder
Protected WithEvents pl1 As System.Web.UI.WebControls.PlaceHolder
Friend WithEvents button2 As Button
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
button1 = New Button
button1.Text = "First Button"
pl1.Controls.Add(button1)
' Add the first button when the page is loaded
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles button1.Click
button2 = New Button
button2.Text = "Second Button"
pl2.Controls.Add(button2)
'add another button when the first button is clicked
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles button2.Click
Response.Redirect("http://www.microsoft.com")
End Sub
'This event is never fired
End Class