如何在C#中动态创建表 [英] How to create table dynamically in c#
本文介绍了如何在C#中动态创建表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<表ID =tableContentBORDER =1=服务器>
&所述; TR>
&所述; TD列跨度=3>
记录1
< / TD>
< / TR>
&所述; TR>
&所述; TD→1&下; / TD>
&所述; TD→2&下; / TD>
&所述; TD&→3所述; / TD>
< / TR>
&所述; TR>
&所述; TD>一种与所述; / TD>
< TD> B< / TD>
&所述; TD&以及c&下; / TD>
< / TR>
&所述; TR>
< TD> M< / TD>
&所述; TD&将N&下; / TD>
< TD> O< / TD>
< / TR>
&所述; TR>
&所述; TD列跨度=3>
<输入ID =Button1的类型=按钮值=键/>
< / TD>
< / TR>
< /表>
我要在C#中动态创建上表
我想,但没有得到
保护无效的Page_Load(对象发件人,EventArgs的发送)
{ HtmlTableRow排= NULL;
HtmlTableCell细胞=无效; 的for(int i = 0;我小于5;我++)
{
行=新HtmlTableRow();
细胞=新HtmlTableCell();
tableContent.Controls.AddAt(我行);
row.Controls.AddAt(ⅰ,细胞);
cell.InnerText =1;
}
}
解决方案
您可以试试这个code创建表。
第一名此标记在你的aspx页面像
<表ID =tableContentBORDER =1=服务器>< /表>
然后试着在Page_Load中这code像
保护无效的Page_Load(对象发件人,EventArgs的发送)
{
HtmlTableRow行=新HtmlTableRow();
HtmlTableCell电池=新HtmlTableCell(); cell.ColSpan = 3;
cell.InnerText =记录1;
row.Cells.Add(细胞);
tableContent.Rows.Add(行); 行=新HtmlTableRow();
细胞=新HtmlTableCell(); cell.InnerText =1;
row.Cells.Add(细胞); 细胞=新HtmlTableCell();
cell.InnerText =2;
row.Cells.Add(细胞); 细胞=新HtmlTableCell();
cell.InnerText =3;
row.Cells.Add(细胞); tableContent.Rows.Add(行); 行=新HtmlTableRow();
细胞=新HtmlTableCell(); cell.InnerText =一个;
row.Cells.Add(细胞); 细胞=新HtmlTableCell();
cell.InnerText =b的;
row.Cells.Add(细胞); 细胞=新HtmlTableCell();
cell.InnerText =C;
row.Cells.Add(细胞); tableContent.Rows.Add(行);
行=新HtmlTableRow();
细胞=新HtmlTableCell();
cell.InnerText =M;
row.Cells.Add(细胞); 细胞=新HtmlTableCell();
cell.InnerText =N的;
row.Cells.Add(细胞); 细胞=新HtmlTableCell();
cell.InnerText =O;
row.Cells.Add(细胞); tableContent.Rows.Add(行); 行=新HtmlTableRow();
细胞=新HtmlTableCell(); HtmlInputButton输入=新HtmlInputButton();
input.ID =Button1的;
input.Value =按钮; cell.ColSpan = 3;
cell.Controls.Add(输入);
row.Cells.Add(细胞);
tableContent.Rows.Add(行);
}
,也可以通过存储在二维数组单元的值,比如
试试这个, 保护无效的Page_Load(对象发件人,EventArgs的发送)
{
串[,] cellValues = {{1,2,3},{一,B,C},{M,N,○}}; HtmlTableRow行=新HtmlTableRow();
HtmlTableCell电池=新HtmlTableCell(); cell.ColSpan = 3;
cell.InnerText =记录1;
row.Cells.Add(细胞);
tableContent.Rows.Add(行); 的for(int i = 0; I< cellValues.GetLength(0);我++)
{
行=新HtmlTableRow();
为(中间体J = 0; J&下; cellValues.GetLength(1); J ++)
{
细胞=新HtmlTableCell();
cell.InnerText = cellValues [I,J]。
row.Cells.Add(细胞);
}
tableContent.Rows.Add(行);
} 行=新HtmlTableRow();
细胞=新HtmlTableCell(); HtmlInputButton输入=新HtmlInputButton();
input.ID =Button1的;
input.Value =按钮; cell.ColSpan = 3;
cell.Controls.Add(输入);
row.Cells.Add(细胞);
tableContent.Rows.Add(行);
}
<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>
I have to create above table dynamically in c# I am trying but didnt get
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";
}
}
解决方案
You can try this code to create table.
First place this markup in your aspx page like
<table id="tableContent" border="1" runat="server"></table>
Then Try This code in Page_Load like
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);
}
Or You can try this, by storing cell values in 2D Array like
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);
}
这篇关于如何在C#中动态创建表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文