Alessandro Zifiglio
2/19/2004 3:31:00 PM
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" <tbsiang1982@hotmail.com> wrote in message
news:%231WcQgv9DHA.2308@TK2MSFTNGP11.phx.gbl...
> 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
>
>