Click here to Skip to main content
Click here to Skip to main content

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

<form runat="server"></form>



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>);

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

Comments and Discussions

Questioncreate dynamic textboxes in web Content Form in PinmemberShama Shahzadi14-Aug-11 2:24 
GeneralShort and sweet PinmemberDon Zocchi2-Feb-06 10:05 
GeneralUse an instance field PinmemberWouter van Vugt23-Nov-05 6:33 

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

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

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