Problems dynamically adding controls to a page

M

Mark Denardo

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
 
B

bruce barker \(sqlwork.com\)

to postback a value, a control must be inside a form. just put a placeholder
on the page.

-- bruce (sqlwork.com)
 
M

Mark Denardo

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

Mark Denardo

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top