473,399 Members | 2,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Problems dynamically adding controls to a page

I'm trying to dynamically create and add controls to a web page:

Label obj1 = new Label();
DropDownList obj2 = new DropDownList();
Controls.Add(obj1);
Controls.Add(obj2);

But I get the following error when the DDL is added (but not the Label):

"Control 'ctl07' of type 'DropDownList' must be placed inside a form tag
with runat=server."

So I saw a web page that added controls like so:

foreach (object item in Page.Controls)
{ if (item.GetType() == typeof(HtmlForm))
{ ((HtmlForm)item).Controls.Add(obj);
break;}}

Which works fine in a normal web page, but when I place my code in a Content
Page, this doesn't work.
Why do controls like Label and Image work ok when adding controls using
Controls.Add, and others give the runat=server error?

How do I get this to work when I add controls in a content page?

Any explanations or reference web pages would be appreciated. -- Mark
Oct 25 '06 #1
3 5455
to postback a value, a control must be inside a form. just put a placeholder
on the page.

-- bruce (sqlwork.com)
"Mark Denardo" <ma**@markdenardo.netwrote in message
news:b6******************************@starstream.n et...
I'm trying to dynamically create and add controls to a web page:

Label obj1 = new Label();
DropDownList obj2 = new DropDownList();
Controls.Add(obj1);
Controls.Add(obj2);

But I get the following error when the DDL is added (but not the Label):

"Control 'ctl07' of type 'DropDownList' must be placed inside a form tag
with runat=server."

So I saw a web page that added controls like so:

foreach (object item in Page.Controls)
{ if (item.GetType() == typeof(HtmlForm))
{ ((HtmlForm)item).Controls.Add(obj);
break;}}

Which works fine in a normal web page, but when I place my code in a
Content Page, this doesn't work.
Why do controls like Label and Image work ok when adding controls using
Controls.Add, and others give the runat=server error?

How do I get this to work when I add controls in a content page?

Any explanations or reference web pages would be appreciated. -- Mark

Oct 25 '06 #2
Well if I plop down a placeholder in the .aspx file, it defeats the purpose
of adding a control dynamically. And if I dynamically create a placeholder
and add the DDL control to it, I get the same error.
"bruce barker (sqlwork.com)" <b_*************************@sqlwork.comwrote
in message news:en****************@TK2MSFTNGP02.phx.gbl...
to postback a value, a control must be inside a form. just put a
placeholder on the page.

-- bruce (sqlwork.com)
"Mark Denardo" <ma**@markdenardo.netwrote in message
news:b6******************************@starstream.n et...
>I'm trying to dynamically create and add controls to a web page:

Label obj1 = new Label();
DropDownList obj2 = new DropDownList();
Controls.Add(obj1);
Controls.Add(obj2);

But I get the following error when the DDL is added (but not the Label):

"Control 'ctl07' of type 'DropDownList' must be placed inside a form tag
with runat=server."

So I saw a web page that added controls like so:

foreach (object item in Page.Controls)
{ if (item.GetType() == typeof(HtmlForm))
{ ((HtmlForm)item).Controls.Add(obj);
break;}}

Which works fine in a normal web page, but when I place my code in a
Content Page, this doesn't work.
Why do controls like Label and Image work ok when adding controls using
Controls.Add, and others give the runat=server error?

How do I get this to work when I add controls in a content page?

Any explanations or reference web pages would be appreciated. -- Mark


Oct 25 '06 #3
Ok, I only need one placeholder for all my added controls. Thought I would
need one per. I guess this will work out me after all.

Thanks - Mark

"Mark Denardo" <ma**@markdenardo.netwrote in message
news:P-******************************@starstream.net...
Well if I plop down a placeholder in the .aspx file, it defeats the
purpose of adding a control dynamically. And if I dynamically create a
placeholder and add the DDL control to it, I get the same error.
"bruce barker (sqlwork.com)" <b_*************************@sqlwork.com>
wrote in message news:en****************@TK2MSFTNGP02.phx.gbl...
>to postback a value, a control must be inside a form. just put a
placeholder on the page.

-- bruce (sqlwork.com)
"Mark Denardo" <ma**@markdenardo.netwrote in message
news:b6******************************@starstream. net...
>>I'm trying to dynamically create and add controls to a web page:

Label obj1 = new Label();
DropDownList obj2 = new DropDownList();
Controls.Add(obj1);
Controls.Add(obj2);

But I get the following error when the DDL is added (but not the Label):

"Control 'ctl07' of type 'DropDownList' must be placed inside a form tag
with runat=server."

So I saw a web page that added controls like so:

foreach (object item in Page.Controls)
{ if (item.GetType() == typeof(HtmlForm))
{ ((HtmlForm)item).Controls.Add(obj);
break;}}

Which works fine in a normal web page, but when I place my code in a
Content Page, this doesn't work.
Why do controls like Label and Image work ok when adding controls using
Controls.Add, and others give the runat=server error?

How do I get this to work when I add controls in a content page?

Any explanations or reference web pages would be appreciated. -- Mark



Oct 25 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kiyomi | last post by:
Hello, I create a Table1 dynamically at run time, and at the same time, I would like to create LinkButton controls, also dynamically, and insert them into each line in my Table1. I would...
4
by: DotNetJunky | last post by:
I have built a control that runs an on-line help system. Depending on the category you selected via dropdownlist, it goes out and gets the child subcategories, and if there are any, adds a new...
8
by: Donald Xie | last post by:
Hi, I noticed an interesting effect when working with controls that are dynamically loaded. For instance, on a web form with a PlaceHolder control named ImageHolder, I dynamically add an image...
1
by: Luurs | last post by:
Hi All, I've been using a asp-label in the HTML <headsection for quite some time in order to dynamically generate meta-keywords,-description, css and js references. Though the actual...
9
by: netasp | last post by:
hi all, how can I populate one aspx form when page is loading based on page ID? for example: loading page A (to search for VB code) would display labels and texboxes, dropdown lists all related...
6
by: | last post by:
I have made some user controls with custom properties. I can set those properties on instances of my user controls, and I have programmed my user control to do useful visual things in response to...
9
by: Chris | last post by:
I am dynamically adding a user control to each row in a gridview. The reason I am doing it dynamically is the user control is different depending on certain data in the gridview. The gridview...
4
by: Lewis Holmes | last post by:
Hi I have the following situation in one of my asp.net pages. The user can add multiple table rows to a form by selecting a button. These rows can contain asp.net controls. When this button is...
1
by: semomaniz | last post by:
I have a form where i have created the form dynamically. First i manually added a panel control to the web page. Then i added another panel dynamically and inside this panel i created tables. I have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.