Adding Html Code dynmically in Placeholder

M

_MC_

Hi,

i want to structure two elements (each conists of 1 Label and 1 Textbox) in
an Table. As I use an Content Place Holder, i thought it is possible to add
the table via Controls.add(Literal). Literal consists of standard html code
like <table>, <tr>, etc.. Nevertheless Asp Net generates me now table in
html code, maybe you see my mistake in the code below. Thanks a lot!

_MC_

Literal tableStart = new Literal(); tableStart.Text =
"<table>";

Literal tableStop = new Literal(); tableStop.Text =
"</table>";

Literal rowStart = new Literal(); rowStart.Text = "<tr>";

Literal rowStop = new Literal(); rowStart.Text = "</tr>";

Literal jumpColumnStart = new Literal(); jumpColumnStart.Text = "<td>";




PlaceHolder2_EditableItems.Controls.Add(tableStart); // <table>

PlaceHolder2_EditableItems.Controls.Add(rowStart); // <tr>

for (int i = 0; i < columnNames.Count; i++) {

lastcount++;

Label infoLabel = new Label();

infoLabel.Text = "test1";


TextBox myValTextBox = new TextBox();

myValTextBox.Text = "test2"



PlaceHolder2_EditableItems.Controls.Add(jumpColumnStart); // <td>

PlaceHolder2_EditableItems.Controls.Add(infoLabel);

PlaceHolder2_EditableItems.Controls.Add(jumpColumnStart); // <td>

PlaceHolder2_EditableItems.Controls.Add(myValTextBox);

if (lastcount == tableColumns)

{

PlaceHolder2_EditableItems.Controls.Add(rowStop); // </tr>

PlaceHolder2_EditableItems.Controls.Add(rowStart); // <tr>

lastcount = 0;

}


}

PlaceHolder2_EditableItems.Controls.Add(tableStop);
 
M

Mark Fitzpatrick

I don't believe it will work as you expect because you are adding the same
object repeatedly. That will confuse the control you are adding it to such
as the placeholder. Your best bet is to create the
literal controls that control the start and stop of the table cells within
the loop. That would limit how often you add the same control to the
placeholder because it will be a new literal control each time it goes
through the loop.
 
B

bruce barker

you need to nest properly. add the tr's to the table controls
collection, td'd to the tr's controls collection, etc.

also there is an HtmlTable controlthat you can use instead.

-- bruce (sqlwork.com)
 

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

Top