By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,288 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,288 IT Pros & Developers. It's quick & easy.

Problems dynamically adding controls to a page

P: n/a
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
Share this Question
Share on Google+
3 Replies

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.