Dynamic control creation

B

Bsiang Tan

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
 
A

Alessandro Zifiglio

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 said:
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
 
B

Bsiang Tan

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top