Creating controls at runtime

Discussion in 'ASP .Net Building Controls' started by geronimo, Jul 22, 2003.

  1. geronimo

    geronimo Guest

    Hi,
    I am trying to create a TextBox at runtime, using the following code with a
    CodeBehind-File:

    Dim txtBox As New System.Web.UI.WebControls.TextBox()
    txtBox.ID ="txtTest"
    Page.Controls.Add(txtBox)

    I am getting an server-error - 'TextBox' has to be used with the FormTag
    runat=server. Where can set the FormTag in a CodeBehind-File?

    Could anyone help me?

    Thank you

    Geronimo
     
    geronimo, Jul 22, 2003
    #1
    1. Advertising

  2. txtbox.runat = "server"

    geronimo wrote:

    > Hi,
    > I am trying to create a TextBox at runtime, using the following code with a
    > CodeBehind-File:
    >
    > Dim txtBox As New System.Web.UI.WebControls.TextBox()
    > txtBox.ID ="txtTest"
    > Page.Controls.Add(txtBox)
    >
    > I am getting an server-error - 'TextBox' has to be used with the FormTag
    > runat=server. Where can set the FormTag in a CodeBehind-File?
    >
    > Could anyone help me?
    >
    > Thank you
    >
    > Geronimo
    >
    >
     
    news.microsoft.com, Jul 22, 2003
    #2
    1. Advertising

  3. geronimo

    Teemu Keiski Guest

    It means TextBox is such control that needs to be inside server-side form at
    ASP.NET Page.

    By default ASPX page's control hierarchy is such that it has three controls
    at first level.

    Page.Controls[0] is a Literal control that has stuff before form tag
    Page.Controls[1] is the HtmlForm that is the server-side form
    Page.Controls[2] is a Literal control that has stuff after form tag

    So you would need to either add the TextBox to the Form by:

    Page.Controls[1].Controls.Add(txtBox)

    or search the HtmlForm on Page's Controls. If it is for example:

    <form id="Form1" method="post" runat="server">

    You can add the TextBox also by:
    Page.FindControl("Form1").Controls.Add(txtBox)

    --
    Teemu Keiski
    MCP, Designer/Developer
    Mansoft tietotekniikka Oy
    http://www.mansoft.fi

    AspInsiders Member, www.aspinsiders.com
    ASP.NET Forums Moderator, www.asp.net
    AspAlliance Columnist, www.aspalliance.com





    "geronimo" <> wrote in message
    news:3f1d10ac$0$12999$-online.net...
    > Hi,
    > I am trying to create a TextBox at runtime, using the following code with

    a
    > CodeBehind-File:
    >
    > Dim txtBox As New System.Web.UI.WebControls.TextBox()
    > txtBox.ID ="txtTest"
    > Page.Controls.Add(txtBox)
    >
    > I am getting an server-error - 'TextBox' has to be used with the FormTag
    > runat=server. Where can set the FormTag in a CodeBehind-File?
    >
    > Could anyone help me?
    >
    > Thank you
    >
    > Geronimo
    >
    >
     
    Teemu Keiski, Jul 22, 2003
    #3
  4. geronimo

    geronimo Guest

    Dear Teemu,

    Thank you for your help - it is working!

    Regards

    Geronimo




    "Teemu Keiski" <> schrieb im Newsbeitrag
    news:#...
    > It means TextBox is such control that needs to be inside server-side form

    at
    > ASP.NET Page.
    >
    > By default ASPX page's control hierarchy is such that it has three

    controls
    > at first level.
    >
    > Page.Controls[0] is a Literal control that has stuff before form tag
    > Page.Controls[1] is the HtmlForm that is the server-side form
    > Page.Controls[2] is a Literal control that has stuff after form tag
    >
    > So you would need to either add the TextBox to the Form by:
    >
    > Page.Controls[1].Controls.Add(txtBox)
    >
    > or search the HtmlForm on Page's Controls. If it is for example:
    >
    > <form id="Form1" method="post" runat="server">
    >
    > You can add the TextBox also by:
    > Page.FindControl("Form1").Controls.Add(txtBox)
    >
    > --
    > Teemu Keiski
    > MCP, Designer/Developer
    > Mansoft tietotekniikka Oy
    > http://www.mansoft.fi
    >
    > AspInsiders Member, www.aspinsiders.com
    > ASP.NET Forums Moderator, www.asp.net
    > AspAlliance Columnist, www.aspalliance.com
    >
    >
    >
    >
    >
    > "geronimo" <> wrote in message
    > news:3f1d10ac$0$12999$-online.net...
    > > Hi,
    > > I am trying to create a TextBox at runtime, using the following code

    with
    > a
    > > CodeBehind-File:
    > >
    > > Dim txtBox As New System.Web.UI.WebControls.TextBox()
    > > txtBox.ID ="txtTest"
    > > Page.Controls.Add(txtBox)
    > >
    > > I am getting an server-error - 'TextBox' has to be used with the FormTag
    > > runat=server. Where can set the FormTag in a CodeBehind-File?
    > >
    > > Could anyone help me?
    > >
    > > Thank you
    > >
    > > Geronimo
    > >
    > >

    >
    >
     
    geronimo, Jul 22, 2003
    #4
    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. Bart Schelkens

    Creating controls at runtime

    Bart Schelkens, Sep 29, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    468
    Lucas Tam
    Sep 29, 2004
  2. Varangian
    Replies:
    7
    Views:
    591
    Varangian
    Apr 27, 2006
  3. Tarscher
    Replies:
    9
    Views:
    423
    Russell
    Feb 13, 2007
  4. Replies:
    2
    Views:
    308
    Scott McPhillips [MVP]
    Nov 7, 2005
  5. Mike Towle

    Dynamically Creating Controls at Runtime

    Mike Towle, Apr 14, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    184
    Alvin Bruney [MVP]
    Apr 16, 2004
Loading...

Share This Page