Dynamically Adding Controls

Discussion in 'ASP .Net Building Controls' started by Nathan Sokalski, May 13, 2005.

  1. 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

    http://www.nathansokalski.com/
    Nathan Sokalski, May 13, 2005
    #1
    1. Advertising

  2. Nathan Sokalski

    Bruce Barker Guest

    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" <> wrote in message
    news:...
    >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
    >
    > http://www.nathansokalski.com/
    >
    Bruce Barker, May 13, 2005
    #2
    1. Advertising

  3. 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


    "Bruce Barker" <> wrote in message
    news:...
    > 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" <> wrote in message
    > news:...
    >>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
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
    Steve C. Orr [MVP, MCSD], May 13, 2005
    #3
  4. 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

    http://www.nathansokalski.com/

    "gabe garza" <> wrote in message
    news:BCahe.16497$...
    > 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" <> wrote in message
    > news:...
    >>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
    >>
    >> http://www.nathansokalski.com/
    >>

    >
    >
    Nathan Sokalski, May 14, 2005
    #4
  5. Nathan Sokalski

    Martin Guest

    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

    "Nathan Sokalski" <> schreef in bericht
    news:%...
    > 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
    >
    > http://www.nathansokalski.com/
    >
    > "gabe garza" <> wrote in message
    > news:BCahe.16497$...
    >> 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" <> wrote in message
    >> news:...
    >>>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
    >>>
    >>> http://www.nathansokalski.com/
    >>>

    >>
    >>

    >
    >
    Martin, May 14, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Wallace
    Replies:
    0
    Views:
    323
    John Wallace
    Aug 6, 2003
  2. MC D
    Replies:
    4
    Views:
    449
    Big D
    Nov 18, 2003
  3. Uzi Baruch
    Replies:
    0
    Views:
    427
    Uzi Baruch
    Dec 17, 2003
  4. ALPO

    Adding Controls Dynamically

    ALPO, Dec 31, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    345
  5. Greg
    Replies:
    1
    Views:
    337
Loading...

Share This Page