我想從代碼添加表格行到一個asp.net表格。我嘗試了下面的代碼。添加了修改後的代碼。但獲取錯誤爲「Sys.WebForms.PageRequestManagerServerErrorException:發生意外錯誤。」動態添加5個表格
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
如何動態地添加錶行?由於
我已經更新了問題中的完整代碼。但仍然出現Sys.WebForms.PageRequestManagerServerErrorException錯誤:發生了意外錯誤 – venkat14
我仍然可以在內部循環中看到'tRow',我認爲這可能不正確。我認爲你應該把'tRow'重命名爲更清楚的東西,這樣你可以看到它是否在不屬於它的地方使用 – Adam
已將它更新爲tRow1。但仍然是相同的問題 – venkat14