Home All Groups Group Topic Archive Search About

Dynamically Adding Controls

Author
13 May 2005 9:48 PM
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.
--
Nathan Sokalski
njsokal***@hotmail.com
http://www.nathansokalski.com/

Author
13 May 2005 10:21 PM
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)


Show quoteHide quote
"Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
news:OOrSAWAWFHA.2196@TK2MSFTNGP09.phx.gbl...
>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.
> --
> Nathan Sokalski
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
Are all your drivers up to date? click for free checkup

Author
13 May 2005 10:58 PM
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.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net


Show quoteHide quote
"Bruce Barker" <brubar_nospamplease_@safeco.com> wrote in message
news:uiOqYoAWFHA.3320@TK2MSFTNGP12.phx.gbl...
> 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)
>
>
> "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
> news:OOrSAWAWFHA.2196@TK2MSFTNGP09.phx.gbl...
>>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.
>> --
>> Nathan Sokalski
>> njsokal***@hotmail.com
>> http://www.nathansokalski.com/
>>
>
>
Author
13 May 2005 11:20 PM
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)


Show quoteHide quote
"Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
news:OOrSAWAWFHA.2196@TK2MSFTNGP09.phx.gbl...
>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.
> --
> Nathan Sokalski
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
Author
13 May 2005 11:56 PM
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.
--
Nathan Sokalski
njsokal***@hotmail.com
http://www.nathansokalski.com/

Show quoteHide quote
"gabe garza" <gbga***@yahoo.com> wrote in message
news:BCahe.16497$J12.2805@newssvr14.news.prodigy.com...
> 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)
>
>
> "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
> news:OOrSAWAWFHA.2196@TK2MSFTNGP09.phx.gbl...
>>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.
>> --
>> Nathan Sokalski
>> njsokal***@hotmail.com
>> http://www.nathansokalski.com/
>>
>
>
Author
14 May 2005 12:25 AM
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

Show quoteHide quote
"Nathan Sokalski" <njsokal***@hotmail.com> schreef in bericht
news:%23X5nsdBWFHA.2424@TK2MSFTNGP10.phx.gbl...
> 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.
> --
> Nathan Sokalski
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
> "gabe garza" <gbga***@yahoo.com> wrote in message
> news:BCahe.16497$J12.2805@newssvr14.news.prodigy.com...
>> 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)
>>
>>
>> "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
>> news:OOrSAWAWFHA.2196@TK2MSFTNGP09.phx.gbl...
>>>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.
>>> --
>>> Nathan Sokalski
>>> njsokal***@hotmail.com
>>> http://www.nathansokalski.com/
>>>
>>
>>
>
>
Author
14 May 2005 12:34 AM
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.


Show quoteHide quote
"Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
news:%23X5nsdBWFHA.2424@TK2MSFTNGP10.phx.gbl...
> 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.
> --
> Nathan Sokalski
> njsokal***@hotmail.com
> http://www.nathansokalski.com/
>
> "gabe garza" <gbga***@yahoo.com> wrote in message
> news:BCahe.16497$J12.2805@newssvr14.news.prodigy.com...
>> 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)
>>
>>
>> "Nathan Sokalski" <njsokal***@hotmail.com> wrote in message
>> news:OOrSAWAWFHA.2196@TK2MSFTNGP09.phx.gbl...
>>>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.
>>> --
>>> Nathan Sokalski
>>> njsokal***@hotmail.com
>>> http://www.nathansokalski.com/
>>>
>>
>>
>
>

Bookmark and Share