Click here to Skip to main content
13,352,408 members (40,860 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 22 Nov 2005

Attach Dynamic Controls to the ASP.NET form correctly

, 22 Nov 2005
Rate this:
Please Sign up or sign in to vote.
Attach Dynamic controls to the Page.Controls collection without using a placeholder control.


Ever tried to create web controls dynamically and add them to the Controls collection of the Page class? Most probably, you would have ended up with an error saying "Control '<control name>' of type '<control type>' must be placed inside a form tag with runat=server."


ASP.NET doesn't place the dynamically generated controls within the




1. Check your in-front file for the ID of the form element. For our example, let the ID be "Form1"
2. In your code-behind, use the following syntax to get a handle to the corresponding HtmlForm object:

HtmlForm form1 = this.FindControl("Form1") as HtmlForm;

3. Add the dynamically generated controls to the controls collection of this control:

form1.Controls.Add(<some control object>);

<p>Voila! It should now work fine. 


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Sundeep Phatak
Web Developer
Australia Australia
No Biography provided

You may also be interested in...


Comments and Discussions

Questioncreate dynamic textboxes in web Content Form in Pin
Shama Shahzadi14-Aug-11 2:24
memberShama Shahzadi14-Aug-11 2:24 
GeneralShort and sweet Pin
Don Zocchi2-Feb-06 10:05
memberDon Zocchi2-Feb-06 10:05 
GeneralUse an instance field Pin
Wouter van Vugt23-Nov-05 6:33
memberWouter van Vugt23-Nov-05 6:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 23 Nov 2005
Article Copyright 2005 by Sundeep Phatak
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid