473,385 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Event firing with dynamically added server controls

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
Nov 18 '05 #1
5 2115
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.
-----Original Message-----
Hi,
I have a scenario where I am dynamically adding a control from code when acontrols event is fired. The problem I have is that when the newly createdcontrol is clicked, the click event does not fire. I have written a simpledemonstration that reproduces the problem and would appreciate if someonecould 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 AsSystem.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 AsSystem.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 AsSystem.EventArgs) Handles button2.Click

Response.Redirect("http://www.microsoft.com")
End Sub

'This event is never fired
End Class
.

Nov 18 '05 #2
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.

"MS Newsgroups" <no****@nospam.com> wrote in message
news:#v**************@TK2MSFTNGP10.phx.gbl...
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

Nov 18 '05 #3
Hi Nicklas,
Try
[http://forums.aspfree.com/t19936/see...771c988f.html]
Hope that helps
Have a great day :)
R. Pooran Prasad
Itreya Technologies Pvt Ltd.,
Mail: po***********@itreya.com.REMOVETHIS
Phone(Off) : 5200179/80/81/82/83 Extn: 42
Mobile: +91 98860 29578
-----Original Message-----
Hi,
I have a scenario where I am dynamically adding a control from code when acontrols event is fired. The problem I have is that when the newly createdcontrol is clicked, the click event does not fire. I have written a simpledemonstration that reproduces the problem and would appreciate if someonecould 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 AsSystem.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 AsSystem.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 AsSystem.EventArgs) Handles button2.Click

Response.Redirect("http://www.microsoft.com")
End Sub

'This event is never fired
End Class
.

Nov 18 '05 #4
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

"MS Newsgroups" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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

Nov 18 '05 #5
Don't forget to add the EventHandler to the newly created control!

--
venlig hilsen / with regards
anders borum
--
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Aymer | last post by:
i created a new button object. how do i attach an event on it? thanx for your help. Dim cc As ColorConverter = new ColorConverter() Dim b = new button() b.ID = "SelectedDate" b.BorderStyle...
3
by: Tim Thomas | last post by:
Hi, I am very new to .NET and am in the process of building my first web application. I will briefly describe what i am trying to achieve: I have a system where suppliers register their...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
2
by: sanju | last post by:
hi there i am trying to fire an button event in ascx file which interacts with database ,my problem is " i am calling ascx file wiht a button click dynamically when user needs to update database...
3
by: JoakimR | last post by:
Hello, I have a problem with events not firing. I've created a web user control which renders a calendar using a table control. In two cells I'm adding LinkButtons for "prev/next month". However,...
4
by: Jordan | last post by:
I need to dynamically add an ImageButton control to a user control and and do some server-side processing when the user clicks it. While I the ImageButton is added to the user control at runtime,...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
7
by: rsaffy | last post by:
I am having trouble with my dynamically created button's event handling. I read that the buttons need to be recreated on every trip to the server, but how exactly do you do that when the datagrid...
2
by: John Kotuby | last post by:
Hi guys, I am converting a rather complicated database driven Web application from classic ASP to ASP.NET 2.0 using VB 2005 as the programming language. The original ASP application works quite...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.