Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Dynamic control creation

Reply
Thread Tools

Dynamic control creation

 
 
Bsiang Tan
Guest
Posts: n/a
 
      02-19-2004
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.S ecurity.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


 
Reply With Quote
 
 
 
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      02-19-2004
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" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.S ecurity.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
>
>



 
Reply With Quote
 
 
 
 
Bsiang Tan
Guest
Posts: n/a
 
      02-19-2004
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.



 
Reply With Quote
 
Alessandro Zifiglio
Guest
Posts: n/a
 
      02-19-2004
Perfect. Your doing it right now. I'm glad i could help

"Bsiang Tan" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> 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.
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic creation of a user control with a dropdownlist Hugo Flores ASP .Net 1 09-06-2006 02:33 AM
Dynamic control creation design issue =?Utf-8?B?UGF1bA==?= ASP .Net 0 02-21-2006 07:45 AM
How to (batch) set EXIF date taken and IPTC creation date and creation time for photos with filenames YYMMDDHHMMSS#.jpg? guercheLE@gmail.com Digital Photography 1 10-04-2005 07:15 PM
Dynamic Control Creation and ViewState: what is the ASP.Net way? PABruceFan ASP .Net 3 07-31-2004 06:43 PM
Dynamic Control Creation w/Events? Jeff Voigt ASP .Net 1 07-08-2003 07:08 PM



Advertisments