Reputation: 1764
How do I take my C# array and write it to an HTML table header?
From what I've read you should be able to use cnippets to get the variable but it's not working...
C# code
protected void Page_Load(object sender, EventArgs e)
{
string[] tableHeaders = {
"Urgency",
"Picker",
"CARD",
"Case Order",
"CR #",
"Patient",
"Age",
"ASA",
"Anes Consult",
"Procedure",
"Booking Surgeon",
"Operating Surgeon",
"Ready Date",
"Ready Time",
"NPO Since",
"EST Time",
"Current Location",
"Postop Need",
"Booked by",
"Special Equipment",
"Comments"
};
}
HTML Code You'll see in my document.write line I try to call the tableHeaders variable. And I use the javascript row variable as a counter.
<thead>
<tr>
<script type="text/javascript">
for (var row = 0; row <= 21; row++) {
document.write("+<%= tableHeaders[+row+] %>+")
}
</script>
</tr>
</thead>
Upvotes: 0
Views: 1867
Reputation: 1764
I shouldn't have been trying to use Java-script to do this. The following worked perfectly.
<%
for (int td = 0; td <= 20; td++)
{
Response.Write("<td>" + tableHeaders[td] + "</td>");
}
%>
Upvotes: 1
Reputation: 37533
Assuming you want to create actual header cells for your table, you should be able to do it all within c# code behind unless you have some need to dump things into JavaScript. Add an ID to your <tr>
element and a runat attribute:
<thead>
<tr ID="headerRow" runat="server">
</tr>
</thead>
Then in your page load you can add each header element individually:
protected void Page_Load(object sender, EventArgs e)
{
string[] tableHeaders = { // blah blah };
foreach(var header in tableHeaders)
{
var tmp = new TableHeaderCell();
tmp.ID = "TH_" + header;
tmp.Text = header;
headerRow.Cells.Add(tmp);
}
}
Then when the page renders, it should render each one out as a <th>
element within your row.
Upvotes: 0
Reputation: 152566
You are creating a local variable in Page_Load
then trying to access it from the page script. You can make it a class member instead:
(outside of Page_Load
or any other method):
private string[] tableHeaders = {
"Urgency",
"Picker",
"CARD",
"Case Order",
"CR #",
"Patient",
"Age",
"ASA",
"Anes Consult",
"Procedure",
"Booking Surgeon",
"Operating Surgeon",
"Ready Date",
"Ready Time",
"NPO Since",
"EST Time",
"Current Location",
"Postop Need",
"Booked by",
"Special Equipment",
"Comments"
};
Upvotes: 0