Runtime Controls Addition.

L

Leon

I am building an application that used the placement of panel controls at runtime. Inside of these panels I would like to add multiple buttons. This is part of a scheduling application that I will not be
reusing (if I can help it). I get an error on the addition of the button controls.... "Must be placed inside a container with runat="server" attribute";


I can not set the runat="server" for a control that is created at runtime (Panel).

Any suggestions?



Sample Code:


Panel objPanel = new Panel();
objPanel.HorizontalAlign = HorizontalAlign.Center;


Button objButton = new Button();
objButton.Text = "Test";

....eventHandler stuff etc.

objPanel.Controls.Add(objButton);
PlaceHolder1.Controls.Add(objPanel);
 
B

Brock Allen

Are you sure the error isn't that they need to be placed in a <form> with
runat=server? My guess is that your Panel is outside the <form runat=server>
in the ASPX page.
 
L

Leon

The panel is being build at runtime and added to a placeholder that is inside of a form with the runat server attribute set. The placeholder also has the runat="server" attribute. I can add buttons all day with no problem until I try to add it to a panel that was created at runtime.
 
L

Lee

Hi ;)

Can't you just set the runat=server attribute using something similar to:

runtimePanel.Attributes.Add ("runat", "server");


Possibly this won't work, just an idea..
 
B

Brock Allen

Can't you just set the runat=server attribute using something similar
to:

runtimePanel.Attributes.Add ("runat", "server");

Possibly this won't work, just an idea..

This won't work. the runat=server is something inspected by the ASP.NET parser
while it's reading your ASPX to construct the class for the page. It's the
directive to tell the parser that the page should contain a control for that
location in the page. At runtime then, to create new controls that weren't
declared in the ASPX (with runat=server) you just need to create them (IOW,
call "new") and then add them into the control hierarchy in the control tree
to say where it should appear to be rendered. The placeholder approach mentioned
is a fine approach.
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top