编写c#数组到aspx html页面
本文关键字:html 页面 aspx 数组 编写 | 更新日期: 2023-09-27 18:14:18
如何将c#数组写入HTML表头?
从我所读到的,你应该能够使用片段来获得变量,但它不工作…
c#代码
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"
};
}
你会在我的文档中看到。我试着调用tableHeaders变量。我使用javascript行变量作为计数器。
<thead>
<tr>
<script type="text/javascript">
for (var row = 0; row <= 21; row++) {
document.write("+<%= tableHeaders[+row+] %>+")
}
</script>
</tr>
</thead>
我不应该尝试使用Java-script来完成此操作。下面的方法非常有效。
<%
for (int td = 0; td <= 20; td++)
{
Response.Write("<td>" + tableHeaders[td] + "</td>");
}
%>
您正在Page_Load
中创建一个本地变量,然后尝试从页面脚本访问它。您可以将其改为类成员:
(在Page_Load
或任何其他方法之外):
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"
};
假设您想要为您的表创建实际的标题单元格,您应该能够在c#代码中完成这一切,除非您需要将内容转储到JavaScript中。添加一个ID到你的<tr>
元素和runat属性:
<thead>
<tr ID="headerRow" runat="server">
</tr>
</thead>
然后在你的页面加载中,你可以单独添加每个页眉元素:
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);
}
}
然后当页面呈现时,它应该在您的行中呈现每个<th>
元素。