473,729 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ob j1);
Controls.Add(ob j2);

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 5489
to postback a value, a control must be inside a form. just put a placeholder
on the page.

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

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

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_************ *************@s qlwork.comwrote
in message news:en******** ********@TK2MSF TNGP02.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**@markdenar do.netwrote in message
news:b6******** *************** *******@starstr eam.net...
>I'm trying to dynamically create and add controls to a web page:

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

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**@markdenar do.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_************ *************@s qlwork.com>
wrote in message news:en******** ********@TK2MSF TNGP02.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**@markdenar do.netwrote in message
news:b6******* *************** ********@starst ream.net...
>>I'm trying to dynamically create and add controls to a web page:

Label obj1 = new Label();
DropDownLis t 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.Ad d, 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
10804
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 then like that, when clicking the LinkButton, the user can be navigated to another page, carrying a variable. I would like to use server.transfer method instead of QueryString as I don't want the carried variable to be visible for the user.
4
5482
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 dropdownlist to the screen for selection. This continues until there are no children, and then it checks for a help article list based on that last selection and displays actual articles for display. Adding the controls and getting everything...
8
4314
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 button at runtime: //----- Code snippet protected System.Web.UI.WebControls.PlaceHolder ImageHolder; private void Page_Load(object sender, System.EventArgs e)
1
3876
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 HTML-output isn't that pretty (<span>'s in the head-section...) it works fine with browsers, crawlers. It never REALLY was an issue...
9
7039
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 to VB, plus the address bar would show something like this: www.somethinghere?id=3 and if you change number 3 from the address bar to (for example) 34 or 71 you would get different page with the same formatting like I.e: www.somethinghere?id=34...
6
11081
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 how those properties are set. I want to be able to do two other things: a) add User control instances to my page, filling in the place of placeholder controls, and b) programmatically setting custom properties on those dynamically spawned...
9
7926
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 contains a placeholder and I add the control to it, the user control is a formview bound to an object datsource. This works great until I post back the page and the user control disappears. What am I doing wrong? Regards, Chris. Protected Sub...
4
4486
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 selected, the row is added using JavaScript. The script uses cloneNode to clone a hidden template row and all of its children and then adds the new row to the table, updates all of the child control Ids and sets visibility etc. The hidden...
1
4909
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 set text boxes and labels inside the table rows. I then added a button. All of these are done through code. The problem that i am having is i can get the value from a text box with resides inside the first panel (out side of panel that is...
0
8763
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9284
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9202
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9148
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8151
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4528
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4796
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.