Dynamic control creation

Discussion in 'ASP .Net Web Controls' started by Bsiang Tan, Feb 19, 2004.

  1. Bsiang Tan

    Bsiang Tan Guest

    Dear all experts,

    I wish to create control like ASP TextBox, Label on the fly. I try
    search MSDN, it point me the following code :

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.Secur
    ityAction.Demand, Name="FullTrust")]
    protected override void CreateChildControls()
    {

    // Add a LiteralControl to the current ControlCollection.
    this.Controls.Add(new LiteralControl("<h3>Value: "));


    // Create a text box control, set the default Text property,
    // and add it to the ControlCollection.
    TextBox box = new TextBox();
    box.Text = "0";
    this.Controls.Add(box);

    this.Controls.Add(new LiteralControl("</h3>"));
    }


    But when I compile and excute the page, it give me error :
    Control '_ctl0' of type 'TextBox' must be placed inside a form tag with
    runat=server.

    Is it I miss something out there ? Can anyone please point me the right way
    ?

    I know the easiest way is to create a place holder and put the control in
    dynamically, but how can I set the control position if I do it in place
    holder ?

    A million thank to you all.

    Best regard,
    Bsiang
    Bsiang Tan, Feb 19, 2004
    #1
    1. Advertising

  2. hi Bsiang,
    Actually a placeholder is excellent if you need control on where your
    controls are being placed dynamically. The error states that your dynamic
    controls be placed within form tags with runat="server" and this is a
    prerequisite for all web controls whether you add them at design time on
    your page or dynamically at runtime.

    so make sure you control is already within form tags. You are working on a
    custom webcontrol if i'm not wrong ? so as you throw this custom control on
    your webform make sure its btw form tags with runat="server"

    I suspect your not after a custom control and that you are performing this
    in your page even though your overriding the createchildcontrols method
    states otherwise, because this method is used primarily by control
    developers, so if your doing all this directly in your page then either use
    a placeholder that is already within your form object with runat="server" or
    get a reference to your form object and add your childcontrols into this
    form object.

    first the declaration :
    Protected WithEvents form1 As System.Web.UI.HtmlControls.HtmlForm

    then add to the forms controls collection instead of adding the control to
    the pages control collection
    form1.controls.add(box)
    This is the easiest way, however you can also get a reference to the form
    object dynamically by looking for it in your pages controls collection.
    Also note, that if you are performing this in your page then why are you
    overriding the createchildcontrols method. This is used mostly in custom
    control development, unless you have some specific requirements ;P

    Also i have noted that you are supplying the text value for your textbox
    first and then adding this to the control and this is wrong. Your textbox
    will never maintain state this way. Even if this has nothing to do with your
    current error this will save you other problems later on.
    box.Text = "0";
    this.Controls.Add(box);

    instead do the opposite :
    this.Controls.Add(box);
    box.Text = "0";



    "Bsiang Tan" <> wrote in message
    news:%...
    > Dear all experts,
    >
    > I wish to create control like ASP TextBox, Label on the fly. I try
    > search MSDN, it point me the following code :
    >
    >

    [System.Security.Permissions.PermissionSet(System.Security.Permissions.Secur
    > ityAction.Demand, Name="FullTrust")]
    > protected override void CreateChildControls()
    > {
    >
    > // Add a LiteralControl to the current ControlCollection.
    > this.Controls.Add(new LiteralControl("<h3>Value: "));
    >
    >
    > // Create a text box control, set the default Text property,
    > // and add it to the ControlCollection.
    > TextBox box = new TextBox();
    > box.Text = "0";
    > this.Controls.Add(box);
    >
    > this.Controls.Add(new LiteralControl("</h3>"));
    > }
    >
    >
    > But when I compile and excute the page, it give me error :
    > Control '_ctl0' of type 'TextBox' must be placed inside a form tag with
    > runat=server.
    >
    > Is it I miss something out there ? Can anyone please point me the right

    way
    > ?
    >
    > I know the easiest way is to create a place holder and put the control in
    > dynamically, but how can I set the control position if I do it in place
    > holder ?
    >
    > A million thank to you all.
    >
    > Best regard,
    > Bsiang
    >
    >
    Alessandro Zifiglio, Feb 19, 2004
    #2
    1. Advertising

  3. Bsiang Tan

    Bsiang Tan Guest

    Dear Alessandro Zifiglio,

    Yes, just like you mentioned, I wish dynamically control creation
    directly in my page. After read your helpful reply..
    I fix my code follow your teaching, and it work. But sincerely hope you can
    check the code for me, so that I won't miss out the thing you teach me ?
    Thank you very very much. Your kindness and generousity is greatly
    appreciated.

    here is my code :

    protected System.Web.UI.HtmlControls.HtmlForm form;

    private void Page_Load(object sender, System.EventArgs e)
    {
    textBox1 = new TextBox();

    form = (HtmlForm) this.FindControl("Form1");
    form.Controls.Add( textBox1 );

    textBox1.Style["POSITION"] = "absolute";
    textBox1.Style["TOP"] = "120";
    }



    Best regard,
    Bsiang Tan.
    Bsiang Tan, Feb 19, 2004
    #3
  4. Perfect. Your doing it right now. I'm glad i could help :)

    "Bsiang Tan" <> wrote in message
    news:%...
    > Dear Alessandro Zifiglio,
    >
    > Yes, just like you mentioned, I wish dynamically control creation
    > directly in my page. After read your helpful reply..
    > I fix my code follow your teaching, and it work. But sincerely hope you

    can
    > check the code for me, so that I won't miss out the thing you teach me ?
    > Thank you very very much. Your kindness and generousity is greatly
    > appreciated.
    >
    > here is my code :
    >
    > protected System.Web.UI.HtmlControls.HtmlForm form;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > textBox1 = new TextBox();
    >
    > form = (HtmlForm) this.FindControl("Form1");
    > form.Controls.Add( textBox1 );
    >
    > textBox1.Style["POSITION"] = "absolute";
    > textBox1.Style["TOP"] = "120";
    > }
    >
    >
    >
    > Best regard,
    > Bsiang Tan.
    >
    >
    >
    Alessandro Zifiglio, Feb 19, 2004
    #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. Jeff Voigt

    Dynamic Control Creation w/Events?

    Jeff Voigt, Jul 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    358
    Natty Gur
    Jul 8, 2003
  2. PABruceFan
    Replies:
    3
    Views:
    4,117
    John Saunders
    Jul 31, 2004
  3. =?Utf-8?B?UGF1bA==?=

    Dynamic control creation design issue

    =?Utf-8?B?UGF1bA==?=, Feb 21, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    420
    =?Utf-8?B?UGF1bA==?=
    Feb 21, 2006
  4. Hugo Flores
    Replies:
    1
    Views:
    490
    daniel #
    Sep 6, 2006
  5. Replies:
    1
    Views:
    338
    Andrew Thompson
    Sep 14, 2006
Loading...

Share This Page