Reputation: 1876
I am finding it much easier to build formatted tables by creating an Excel worksheet with the desired appearance, then SaveAs->Web Page (html)->Selection->Publish and copying the table HTML into my .aspx page. I wanted to change a couple of the cell values from the code-behind page and I couldn't find an example on the web. Maybe it's just too obvious, or maybe nobody else is trying to do such a dumb thing?
Upvotes: 0
Views: 5672
Reputation: 1876
For what it's worth, here is my simple solution:
<table id="tblScoreCard" runat='server'>
<tr>
<td>A static cell</td>
<td id='celUpdate' runat='server'>Dynamic cell value</td>
<td>Another static cell</td>
</tr>
</table>
I have added an 'id' attribute to the Excel-generated table HTML and added the 'runat' attribute set to 'server' as well. The table 'id' attribute may be optional, but 'runat' is definitely not. Most of the cells are left as they came out of Excel. Any that I want to change from code behind get the 'id' and 'runat' attribute treatment. Setting them from code behind is easy:
this.celUpdate.InnerText = "Value set at run-time.";
If you want formatted HTML inside the cell, use InnerHtml instead of InnerText. I haven't tried others, but I imagine that could include adding any kind of HTML element in the cell beyond just putting in a <br/>
tag like I'm doing.
EDIT
With the table attribute runat='server', you can also reference by row and column:
this.tblUpdate.Rows[0].Cells[0].InnerText = "Test of dynamic update";
Upvotes: 2