Dynamically Adding Controls

N

Nathan Sokalski

I am trying to dynamically add controls to my page, but am having trouble
with controls such as buttons. I have been able to add simple controls such
as Label controls, because they can be placed anywhere. I have managed to
add Labels using the following code:

Dim extralabel As Label = New Label

extralabel.Text = "Generated Label"

Me.Controls.Add(extralabel)


This places the label at the very end of the generated code inside SPAN
tags. However, when I try something similar with a Button control using the
following code:

Dim extrabutton As Button = New Button

extrabutton.Text = "Generated Button"

Me.Controls.Add(extrabutton)

I recieve an error telling me that a Button control must be between FORM
tags (which makes sense, since a Button control generates an INPUT tag). I
am having trouble figuring out how to add the Button control so that it is
between the form tags. Does anyone know how to do this? A simple example
would be nice. Any help would be appreciated. Thanks.
 
B

Bruce Barker

the most common approach is to add a placeholder control on the form, that
you add the button to. but you can loop thru this.Controls, looking for the
form (check type), then add the button to the form control.

-- bruce (sqlwork.com)
 
S

Steve C. Orr [MVP, MCSD]

I agree with Bruce about the most common approach.
You can also add it to the control collection of any other container control
that might conveniently be in the right place, such as a panel or table
cell.
 
G

gabe garza

You Add() to an instance of System.Web.UI.HtmlControls.HtmlForm for BUTTONS.
When you use "Me" that's the PAGE not a FORM.

If you have a FORM on your PAGE called:
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm

Then you'd add BUTTON controls using:
Dim extrabutton As Button = New Button
extrabutton.Text = "Generated Button"
Form1.Controls.Add(extrabutton)
 
N

Nathan Sokalski

That sounds like it would work when I already have a named form, but I just
want to add a button to the same form as the buttons that are hard-coded in
the design. Because that form is not created until runtime, I have no way to
know what it will be named (it has been given the name "Form1" when I "View
Source" from my browser, but how do I know that will not be changed?). Is
their a way to refer to this main form without using a name? Thanks.
 
M

Martin

In the PageLoad event u can add controls to the form or some
placeholder(preferred)

after the main PageLoad event the load events for each added control are
fired

No trouble with rendering at this points

hope this helps
Martin
 
G

gabe garza

If Visual Studio 2003 look in your aspx code behind (whatever.aspx.vb) for
the following section
#Region " Web Form Designer Generated Code "

Once you locate it see if you see a HtmlForm entry like the following:
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm

If not, then you need to look at your ASPX page (whatever.aspx) and view it
in HTML not in DESIGN.
Locate for your <form> tag, once you see it does it have a ID attribute. If
not add one. If you name it as follows:
<form id="Form1" name="Form1" method="post" runat="server">

Then go back to your code behind (whatever.aspx.vb) in the #Region section
and add the following:
Protected WithEvents Form1 As System.Web.UI.HtmlControls.HtmlForm

Once you do that add the following code in your Page_Load() Event
Dim extrabutton As Button = New Button
extrabutton.Text = "Generated Button"
Form1.Controls.Add(extrabutton)

That will add your BUTTON dynamicly.

Your FORM called Form1 is now associated with your "hard coded" controls
built with the designer as well as your dynamic control you added with VB.

I just tried it to an existing application I have and it does work.
If you have problems please post your code.
 

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,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top