我正在尝试从代码中将表行添加到asp.net表中。我尝试了下面的代码。添加了修改后的代码。但是将错误收到" Sys.WebForms.PageRequestManagerServerErrorException:发生意外错误。"
int rowCnt = 5;
// Current row count.
int rowCtr;
// Total number of cells per row (columns).
int cellCtr;
// Current cell counter.
int cellCnt = 4;
int ctr=0;
ctr = ctr + 1;
foreach (){
TableRow tRow = new TableRow();
table.Rows.Add(tRow1);
TableCell artifactCell = new TableCell();
artifactCell.ColumnSpan = 4;
tRow.Cells.Add(artifactCell);
artifactCell.Controls.Add(new LiteralControl("Artifact " + ctr + " : "));
// Create a Hyperlink Web server control and add it to the cell.
System.Web.UI.WebControls.HyperLink h = new HyperLink();
h.Text = artifact.LookupValue;
artifactCell.Controls.Add(h);
TableCell uploadCell = new TableCell();
uploadCell.ColumnSpan = 4;
tRow.Cells.Add(uploadCell);
FileUpload fu = new FileUpload();
fu.ID = "fu_" + BU +"_" + artifact.LookupValue + artifact.LookupId;
fu.AllowMultiple = false;
Button btnUpload = new Button();
btnUpload.ID = "btnUpload_"+ BU + "_" + artifact.LookupValue + artifact.LookupId;
btnUpload.Text = "Upload";
btnUpload.Click += new EventHandler(this.btnUpload_Click);
uploadCell.Controls.Add(fu);
uploadCell.Controls.Add(btnUpload);
for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
{
// Create a new row and add it to the table.
TableRow tRow1 = new TableRow();
table.Rows.Add(tRow1);
for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
tRow.Cells.Add(tCell);
// Create a Hyperlink Web server control and add it to the cell.
System.Web.UI.WebControls.HyperLink hyp = new HyperLink();
hyp.Text = rowCtr.ToString();
hyp.ID = "hyp_" + BU + "_" + artifact.LookupValue + artifact.LookupId;
hyp.NavigateUrl = "http://www.microsoft.com/net";
tCell.Controls.Add(hyp);
HiddenField hid = new HiddenField();
hid.ID = "hdn_" + BU + "_" + artifact.LookupValue + artifact.LookupId;
tCell.Controls.Add(hid);
Label lbll = new Label();
lbll.ID = "lblError_"+ BU + "_" + artifact.LookupValue + artifact.LookupId;
lbll.ForeColor = System.Drawing.Color.Red;
tCell.Controls.Add(lbll);
}
}
这是创建超链接和隐藏字段。但是它如下所示排在一行,数据以超链接显示
1:1 1:2 1:3 1:4 2:1 2:2 2:3 2:4 3:1 3:2 3:3 3:4 4:1 4:2 4:3 4:4 5:1 5:2 5:3 5:4
我想要实现的目标是:
First TR
hyperlink 1
hidden field 1
Second TR
hyperlink2
hidden field 2
如何动态添加表格行?感谢
答案 0 :(得分:1)
除了@Adam提到的拼写错误之外,将tRow1更改为tRow。你的代码似乎工作: 输出是这样的:
1:1 1:2 1:3 1:4
2:1 2:2 2:3 2:4
3:1 3:2 3:3 3:4
4:1 4:2 4:3 4:4
5:1 5:2 5:3 5:4
更新: 在不进行查找的情况下将内循环更改为静态值。这是代码:
for (rowCtr = 1; rowCtr <= rowCnt; rowCtr++)
{
// Create a new row and add it to the table.
TableRow tRow1 = new TableRow();
table.Rows.Add(tRow1);
for (cellCtr = 1; cellCtr <= cellCnt; cellCtr++)
{
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
tRow1.Cells.Add(tCell);
// Create a Hyperlink Web server control and add it to the cell.
System.Web.UI.WebControls.HyperLink hyp = new HyperLink();
hyp.Text = rowCtr.ToString();
hyp.ID = "hyp_"+rowCtr+cellCtr;
hyp.NavigateUrl = "http://www.microsoft.com/net";
tCell.Controls.Add(hyp);
HiddenField hid = new HiddenField();
hid.ID = "hdn_" + rowCtr + cellCtr;
tCell.Controls.Add(hid);
Label lbll = new Label();
lbll.ID = "lblError_" + rowCtr + cellCtr;
lbll.ForeColor = System.Drawing.Color.Red;
tCell.Controls.Add(lbll);
}
}
答案 1 :(得分:0)
以下代码看起来好像包含错误:
TableRow tRow1 = new TableRow();
table.Rows.Add(tRow);
在循环中将tRow
更改为tRow1
,看起来应该可以正常工作
tRow
和tRow1
不是最好的变量名称。这很可能是你问题的最终根源,选择更具描述性的名字!