Event firing with dynamically added server controls

  • Thread starter Thread starter MS Newsgroups
  • Start date Start date
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
 
ASP.Net Pages are not persisted across PostBacks due to the stateless nature
of HTTP. Therefore, when you add a Control to a WebForm, and wire up an
event handler for the control, when the next PostBack occurs, the Control
will not be there unless you add it back in again (since it was not defined
in the Page's Class definition). As the LoadPostData event handler runs
prior to the Page_Load event handler, you must re-add the Control prior to
the execution of the LoadPostData Method, if you want it to fire and handle
events. This can be done in the OnInit event handler or the LoadViewState
Method.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
I'm a bit rusty with VB.NET but i'll give this a try.

First of all where are you wiring the events with the
handler?

Common practise for dynamically added controls is that you
have to wire the events everytime there's a postback in
order for it to fire and be handled properly.

HTH,
Suresh.
 
Kevin,

Thanks for the explanation I can see why this is not working now, but
unfortunately I have not been able to find a solution.

I have added the fllowing code to my previous example.

In the button1 click event I have added

Session.Add("button2", button2)

Then in have overridden the onInit event with the floowing code, but the
event is still not firing.

Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
If Not Session.Item("button2") Is Nothing Then
button2 = New Button
End If
End Sub

Any suggestions on what I am doing wrong ?

Regards

Niclas
 
Back
Top