469,641 Members | 1,187 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,641 developers. It's quick & easy.

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 5288
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Donald Xie | last post: by
9 posts views Thread by netasp | last post: by
9 posts views Thread by Chris | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.