Click here to Skip to main content
12,629,978 members (23,183 online)
Click here to Skip to main content
Add your own
alternative version

Stats

28.9K views
172 downloads
23 bookmarked
Posted

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.

Introduction

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."

Reason


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

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

tags.

Resolution


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.

License

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

Share

About the Author

Sundeep Phatak
Web Developer
Australia Australia
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questioncreate dynamic textboxes in web Content Form in asp.net? 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.

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