<table id="tableContent" border="1" runat="server">
<tr>
<td colspan="3">
Record 1
</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
</tr>
<tr>
<td>a</td>
<td>b</td>
<td>c</td>
</tr>
<tr>
<td>m</td>
<td>n</td>
<td>o</td>
</tr>
<tr>
<td colspan="3">
<input id="Button1" type="button" value="button" />
</td>
</tr>
</table>
我必须在 C# 中动态创建上表
我正在尝试但没有得到
protected void Page_Load(object sender, EventArgs e)
{
HtmlTableRow row = null;
HtmlTableCell cell = null;
for(int i = 0; i < 5; i++)
{
row = new HtmlTableRow();
cell = new HtmlTableCell();
tableContent.Controls.AddAt(i, row);
row.Controls.AddAt(i, cell);
cell.InnerText="1";
}
}
您可以尝试使用此代码来创建表。
首先将此标记放置在您的 aspx 页面中,例如
<table id="tableContent" border="1" runat="server"></table>
然后在 Page_Load 中尝试此代码,例如
protected void Page_Load(object sender, EventArgs e)
{
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
cell.ColSpan =3;
cell.InnerText = "Record 1";
row.Cells.Add(cell);
tableContent.Rows.Add(row);
row = new HtmlTableRow();
cell = new HtmlTableCell();
cell.InnerText = "1";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = "2";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = "3";
row.Cells.Add(cell);
tableContent.Rows.Add(row);
row = new HtmlTableRow();
cell = new HtmlTableCell();
cell.InnerText = "a";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = "b";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = "c";
row.Cells.Add(cell);
tableContent.Rows.Add(row);
row = new HtmlTableRow();
cell = new HtmlTableCell();
cell.InnerText = "m";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = "n";
row.Cells.Add(cell);
cell = new HtmlTableCell();
cell.InnerText = "o";
row.Cells.Add(cell);
tableContent.Rows.Add(row);
row = new HtmlTableRow();
cell = new HtmlTableCell();
HtmlInputButton input = new HtmlInputButton();
input.ID = "Button1";
input.Value = "button";
cell.ColSpan = 3;
cell.Controls.Add(input);
row.Cells.Add(cell);
tableContent.Rows.Add(row);
}
或者您可以尝试此操作,将单元格值存储在二维数组中,例如
protected void Page_Load(object sender, EventArgs e)
{
String[,] cellValues = { { "1", "2", "3" }, { "a", "b", "c" }, { "m", "n", "o" } };
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell = new HtmlTableCell();
cell.ColSpan = 3;
cell.InnerText = "Record 1";
row.Cells.Add(cell);
tableContent.Rows.Add(row);
for (int i = 0; i < cellValues.GetLength(0); i++)
{
row = new HtmlTableRow();
for (int j = 0; j < cellValues.GetLength(1); j++)
{
cell = new HtmlTableCell();
cell.InnerText = cellValues[i, j];
row.Cells.Add(cell);
}
tableContent.Rows.Add(row);
}
row = new HtmlTableRow();
cell = new HtmlTableCell();
HtmlInputButton input = new HtmlInputButton();
input.ID = "Button1";
input.Value = "button";
cell.ColSpan = 3;
cell.Controls.Add(input);
row.Cells.Add(cell);
tableContent.Rows.Add(row);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)