Reputation: 977
I am attempting to build a table dynamically when a user hits a button. What i am trying to do is have it so that it will first build the headers of the table.
My code for trying to do this is below. So when a user hits the create table button this method is run:
private void AddTableTitles()
{
Literal startRow = new Literal();
startRow.Text = "<tr><th colspan=\"2\">";
Literal Column = new Literal();
Column.Text = "</th><th colspan=\"2\">";
Literal endRow = new Literal();
endRow.Text = "</th></tr>";
AssignPlaceHolder.Controls.Add(startRow);
AssignPlaceHolder.Controls.Add(AddSiteHeader());
AssignPlaceHolder.Controls.Add(Column);
AssignPlaceHolder.Controls.Add(AddMachNameHeader());
AssignPlaceHolder.Controls.Add(Column);
AssignPlaceHolder.Controls.Add(AddMachTypeHeader());
AssignPlaceHolder.Controls.Add(Column);
AssignPlaceHolder.Controls.Add(AddMachModelHeader());
AssignPlaceHolder.Controls.Add(endRow);
}
NOTE: AssignPlaceHolder just puts the given item into the page. The code for that is here:
public PlaceHolder AssignPlaceHolders
{
get { return this.AssignPlaceHolder; }
}
So what i am trying to get is something like this.
Site | Machine Name | Machine Type | Machine Model
as the table headers however, this is what i end up getting:
SiteMachine NameMachine Type | Machine Model
Note: the "|" represents the split between columns that you should get in the table.
As you can see it will only split it up into the two columns. What exactly am i doing wrong and how can i fix this?
Note: I know i can hard code this part, but by figuring out how to get this part to work in this manner i will then be able to fix the problem i am having with the dynamic section of my table that has the amount of items fed into it determined by the amount of machines stored in the database which has the same thing happening to it as well.
Update1 as per request
private Label AddSiteHeader()
{
Label label = new Label();
label.ForeColor = System.Drawing.Color.DarkBlue;
label.Text = "Site ";
return label;
}
All those methods do is create a label and populate the text for it.
Upvotes: 0
Views: 5799
Reputation: 977
Okay so i figured it out with using the placeholder i had previously, so i figured i would post it in case anyone else has this issue.
private void AddTableTitles()
{
TableHeaderCell site = new TableHeaderCell();
TableHeaderCell name = new TableHeaderCell();
TableHeaderCell type = new TableHeaderCell();
TableHeaderCell model = new TableHeaderCell();
TableHeaderRow th = new TableHeaderRow();
site.Controls.Add(AddSiteHeader());
th.Controls.Add(site);
//AssignPlaceHolder.Controls.Add(th);
name.Controls.Add(AddMachNameHeader());
th.Controls.Add(name);
//AssignPlaceHolder.Controls.Add(th);
type.Controls.Add(AddMachTypeHeader());
th.Controls.Add(type);
//AssignPlaceHolder.Controls.Add(th);
model.Controls.Add(AddMachModelHeader());
th.Controls.Add(model);
AssignPlaceHolder.Controls.Add(th);
}
it ended up being a simple problem which only needed to have new tablecells added for each column.
Upvotes: 1
Reputation: 47726
Would a Repeater
be more useful in this case?
<asp:Repeater ID="yourTable" runat="server">
<HeaderTemplate>
<table>
<tr>
<th><%= AddSiteHeader() %></th>
<th><%= AddMachTypeHeader() %></th>
<th><%= AddMachModelHeader()%></th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("Site") %></td>
<td><%# Eval("MachType") %></td>
<td><%# Eval("MachModel") %></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
You can bind it to your datasource and save you a whole lot of work because this is doing what you are doing. If you just need the HTML output of the Repeater
and you want to serve the output and push it into a div or something you could pre-render it and get the HTML output using the RenderControl
method.
Seems like how the solution is working is using the old ASP way of thinking, not ASP.NET and leveraging it's strengths.
Upvotes: 2