Dynamically Adding Controls

Ask a Question related to ASP.NET Building Controls, Design and Development.

  1. #1

    Default Dynamically Adding Controls

    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
    [email]njsokalski@hotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]


    Nathan Sokalski Guest

  2. Similar Questions and Discussions

    1. Adding controls dynamically to a datagrid
      Hi, We have a requirement, where based on the UI type selected in a dropdown, a control should get added to the column of a grid dynamically. For...
    2. Dynamically adding controls to a data repeater
      I would like to create a composite control based on a data repeater how do I dynamically add and bind child controls to it? I imagine this is not a...
    3. dynamically adding user controls
      I have a web form that has a button called "Add Blank Row". Every time this button is pressed a new "blank row" user control should be added to the...
    4. Dynamically adding controls to page SOURCE file
      Hi, I have a class, derived from "Control" which I can include on a page using: <tag:MyControl attr1=value></tag:MyControl> My questions are:...
    5. Event not firing. Adding controls dynamically to UserControl
      I am adding controls to the UserControl dynamically and then loading the UserControl Dynamically.But I am facing problem with firing of click event...
  3. #2

    Default Re: Dynamically Adding Controls

    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" <njsokalski@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
    > [email]njsokalski@hotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >

    Bruce Barker Guest

  4. #3

    Default Re: Dynamically Adding Controls

    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
    [url]http://SteveOrr.net[/url]


    "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" <njsokalski@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
    >> [email]njsokalski@hotmail.com[/email]
    >> [url]http://www.nathansokalski.com/[/url]
    >>
    >
    >

    Steve C. Orr [MVP, MCSD] Guest

  5. #4

    Default Re: Dynamically Adding Controls

    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" <njsokalski@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
    > [email]njsokalski@hotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >

    gabe garza Guest

  6. #5

    Default Re: Dynamically Adding Controls

    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
    [email]njsokalski@hotmail.com[/email]
    [url]http://www.nathansokalski.com/[/url]

    "gabe garza" <gbgarza@yahoo.com> wrote in message
    news:BCahe.16497$J12.2805@newssvr14.news.prodigy.c om...
    > 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" <njsokalski@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
    >> [email]njsokalski@hotmail.com[/email]
    >> [url]http://www.nathansokalski.com/[/url]
    >>
    >
    >

    Nathan Sokalski Guest

  7. #6

    Default Re: Dynamically Adding Controls

    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" <njsokalski@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
    > [email]njsokalski@hotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >
    > "gabe garza" <gbgarza@yahoo.com> wrote in message
    > news:BCahe.16497$J12.2805@newssvr14.news.prodigy.c om...
    >> 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" <njsokalski@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
    >>> [email]njsokalski@hotmail.com[/email]
    >>> [url]http://www.nathansokalski.com/[/url]
    >>>
    >>
    >>
    >
    >

    Martin Guest

  8. #7

    Default Re: Dynamically Adding Controls

    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.


    "Nathan Sokalski" <njsokalski@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
    > [email]njsokalski@hotmail.com[/email]
    > [url]http://www.nathansokalski.com/[/url]
    >
    > "gabe garza" <gbgarza@yahoo.com> wrote in message
    > news:BCahe.16497$J12.2805@newssvr14.news.prodigy.c om...
    >> 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" <njsokalski@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
    >>> [email]njsokalski@hotmail.com[/email]
    >>> [url]http://www.nathansokalski.com/[/url]
    >>>
    >>
    >>
    >
    >

    gabe garza Guest

  9. #8

    Default Dynamically adding controls

    Hi all,
    I currently have a datagrid with a buttoncolumn and a few other boundcolumns.
    Once a user clicks on a button in the buttoncolumn, I need to remove that
    control and put another control in it's place. I am having no luck
    whatsoever in doing this. I have made a new LinkButton in my code-behind (C#)
    and have tried adding it to the calling row (e.item.cells[0].controls.add
    (newControl)) - this seems to work as far as placing the control there.
    However, once I click on it the event does not fire. I've tried a Click
    event, a Command event - neither of those worked. What I really need is for
    it to fire an event where I can get the DataGridCommandEventArgs for the
    particular row that was clicked. I tried adding the control to the Datagrid,
    but my button ends up outside the table (probably because I don't know how to
    tell it to put it in the right place).

    So I suppose my question would be:
    Is there a way to add a linkbutton in the code-behind to a datagrid such that
    it will raise the ItemClick event (and I will be able to get the
    DataGridCommandEventArgs from it)?

    Thanks in advance.
    -TR

    --
    Message posted via DotNetMonster.com
    [url]http://www.dotnetmonster.com/Uwe/Forums.aspx/asp-net-datagrid/200511/1[/url]
    treilly Guest

  10. #9

    Default Re: Dynamically Adding Controls


    What you could also do is use the syntax (assume the name of your form
    in the ASPX file is Form1)in C#:

    HtmlForm form1 = this.FindControl("Form1") as HtmlForm;

    This gives you a handle to the form and you add controls dynamically to
    the form's controls collection.

    It also doesn't force you to place an unneccesary placeholder control.


    Hope this helps.



    --
    SundeepP
    ------------------------------------------------------------------------
    Posted via [url]http://www.codecomments.com[/url]
    ------------------------------------------------------------------------

    SundeepP Guest

  11. #10

    Default Re: Dynamically adding controls

    Are you handling the ItemCommand event?
    --------------------------------------------------------------------------------
    All that glitters has a high refractive index.
    [url]www.mendhak.com[/url]

    "treilly" <u15154@uwe> wrote in message news:57b35027a5184@uwe...
    > Hi all,
    > I currently have a datagrid with a buttoncolumn and a few other
    > boundcolumns.
    > Once a user clicks on a button in the buttoncolumn, I need to remove that
    > control and put another control in it's place. I am having no luck
    > whatsoever in doing this. I have made a new LinkButton in my code-behind
    > (C#)
    > and have tried adding it to the calling row (e.item.cells[0].controls.add
    > (newControl)) - this seems to work as far as placing the control there.
    > However, once I click on it the event does not fire. I've tried a Click
    > event, a Command event - neither of those worked. What I really need is
    > for
    > it to fire an event where I can get the DataGridCommandEventArgs for the
    > particular row that was clicked. I tried adding the control to the
    > Datagrid,
    > but my button ends up outside the table (probably because I don't know how
    > to
    > tell it to put it in the right place).
    >
    > So I suppose my question would be:
    > Is there a way to add a linkbutton in the code-behind to a datagrid such
    > that
    > it will raise the ItemClick event (and I will be able to get the
    > DataGridCommandEventArgs from it)?
    >
    > Thanks in advance.
    > -TR
    >
    > --
    > Message posted via DotNetMonster.com
    > [url]http://www.dotnetmonster.com/Uwe/Forums.aspx/asp-net-datagrid/200511/1[/url]

    S.M. Altaf [MVP] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139