generated buttons

G

Guest

using this usercontrol
- http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx -
I manage to create several inputfields and buttons, depending on what user
needs.
works fine

However, I do not find much useable help on recreating eventhandlers for
dynamic generated buttons. All I seem to find is when you know in advance how
much buttons will be created, which unfortunately is not the case in my
application

anyone an idea ?
 
K

Ken Cox [Microsoft MVP]

Salut Benoit,

If you are creating buttons dynamically, you need to use Addhandler to
create the event and tell it the name of the routine that handles the event.

AddHandler btnBtn.Click, AddressOf displayit

I've pasted in some code below that creates buttons in a loop and adds
handlers for each.

Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

Dim ee As New EventArgs
Private Sub Page_Load _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim txtBx As TextBox
Dim btnBtn As Button
Dim lblLbl As Label
Dim litLiteral As Literal
Dim intCounter As Integer

For intCounter = 1 To 5
btnBtn = New Button
btnBtn.Text = "Button" & intCounter.ToString
lblLbl = New Label
lblLbl.Text = "Label" & intCounter.ToString
litLiteral = New Literal
litLiteral.Text = "<br>"
PlaceHolder1.Controls.Add(litLiteral)
PlaceHolder1.Controls.Add(lblLbl)
PlaceHolder1.Controls.Add(litLiteral)
ee.Equals(lblLbl)
AddHandler btnBtn.Click, AddressOf displayit
PlaceHolder1.Controls.Add(btnBtn)
PlaceHolder1.Controls.Add(litLiteral)
Next

End Sub
Public Sub displayit(ByVal s As Object, ByVal e As EventArgs)
Dim btn As Button
btn = s

Label1.Text = btn.ClientID & _
": Dynamically Added Link Button is Clicked"
End Sub

<form id="Form1" method="post" runat="server">
<p>
<asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder></p>
<p>
<asp:label id="Label1" runat="server">Label</asp:label></p>
</form>
 
J

JV

If you drop a button on a form, add a CLICK handler then look at your code,
you will be able to see exactly how it's done. You'll just have to do
likewise in your code for dynamically created buttons. Look for a section
in your code as follows:

#region Web Form Designer generated code

You will find a function called InitializeComponent() which has some code
that sets up your event handler. For example, for your PAGE_LOAD event, you
will see something like:
this.Load += new System.EventHandler(this.Page_Load);

That is how you tell the control to call your function.
 
G

Guest

Just in case people still have issues... I was doing a similar thing where I
built a treeview hierarchy with CheckBoxes dynamically. When I added the
event handlers, they did not fire UNTIL i added the handlers BEFORE I added
the control to the parent!!!

It seems that when you add the control to the parent's collection, event
handlers must have already been setup on the dynamic control.

Ken Cox said:
Salut Benoit,

If you are creating buttons dynamically, you need to use Addhandler to
create the event and tell it the name of the routine that handles the event.

AddHandler btnBtn.Click, AddressOf displayit

I've pasted in some code below that creates buttons in a loop and adds
handlers for each.

Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]
Toronto

Dim ee As New EventArgs
Private Sub Page_Load _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim txtBx As TextBox
Dim btnBtn As Button
Dim lblLbl As Label
Dim litLiteral As Literal
Dim intCounter As Integer

For intCounter = 1 To 5
btnBtn = New Button
btnBtn.Text = "Button" & intCounter.ToString
lblLbl = New Label
lblLbl.Text = "Label" & intCounter.ToString
litLiteral = New Literal
litLiteral.Text = "<br>"
PlaceHolder1.Controls.Add(litLiteral)
PlaceHolder1.Controls.Add(lblLbl)
PlaceHolder1.Controls.Add(litLiteral)
ee.Equals(lblLbl)
AddHandler btnBtn.Click, AddressOf displayit
PlaceHolder1.Controls.Add(btnBtn)
PlaceHolder1.Controls.Add(litLiteral)
Next

End Sub
Public Sub displayit(ByVal s As Object, ByVal e As EventArgs)
Dim btn As Button
btn = s

Label1.Text = btn.ClientID & _
": Dynamically Added Link Button is Clicked"
End Sub

<form id="Form1" method="post" runat="server">
<p>
<asp:placeholder id="PlaceHolder1"
runat="server"></asp:placeholder></p>
<p>
<asp:label id="Label1" runat="server">Label</asp:label></p>
</form>

benoit said:
using this usercontrol
-
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx -
I manage to create several inputfields and buttons, depending on what user
needs.
works fine

However, I do not find much useable help on recreating eventhandlers for
dynamic generated buttons. All I seem to find is when you know in advance
how
much buttons will be created, which unfortunately is not the case in my
application

anyone an idea ?
 

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,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top