Board index » Web Programming » Problems dynamically adding controls to a page

Problems dynamically adding controls to a page

Web Programming306
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


-
 

Re:Problems dynamically adding controls to a page

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

on the page.



-- bruce (sqlwork.com)





"Mark Denardo" <mark@markdenardo.net>wrote in message

Quote
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







-

Re:Problems dynamically adding controls to a page

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_r_u_c_e_removeunderscores@sqlwork.com>wrote

in message news:enB9v5G%23GHA.3644@TK2MSFTNGP02.phx.gbl...

Quote
to postback a value, a control must be inside a form. just put a

placeholder on the page.



-- bruce (sqlwork.com)





"Mark Denardo" <mark@markdenardo.net>wrote in message

news:b6edndQzp77uO6LYnZ2dnUVZ_tKdnZ2d@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

>









-

Re:Problems dynamically adding controls to a page

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" <mark@markdenardo.net>wrote in message

Quote
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_r_u_c_e_removeunderscores@sqlwork.com>

wrote in message news:enB9v5G%23GHA.3644@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" <mark@markdenardo.net>wrote in message

>news:b6edndQzp77uO6LYnZ2dnUVZ_tKdnZ2d@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

>>

>

>









-