2017-10-05 74 views
1

我想從代碼添加表格行到一個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 

如何動態地添加錶行?由於

回答

0

下面的代碼看起來好像它包含一個錯誤:

TableRow tRow1 = new TableRow(); 
table.Rows.Add(tRow); 

變化tRow在循環tRow1,它看起來像它應該工作

tRowtRow1是不是最好的變量名。這很可能是你問題的根源,選擇了更多的描述性名字!

+0

我已經更新了問題中的完整代碼。但仍然出現Sys.WebForms.PageRequestManagerServerErrorException錯誤:發生了意外錯誤 – venkat14

+0

我仍然可以在內部循環中看到'tRow',我認爲這可能不正確。我認爲你應該把'tRow'重命名爲更清楚的東西,這樣你可以看到它是否在不屬於它的地方使用 – Adam

+0

已將它更新爲tRow1。但仍然是相同的問題 – venkat14

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); 

      } 

     } 
+0

我收到錯誤爲Sys.WebForms.PageRequestManagerServerErrorException:發生意外的錯誤。 – venkat14

+0

同樣@Adam在評論中回覆了內部循環中的tRow1僅被添加爲空,而您將所有單元添加到tRow。將單元格循環中的所有tRow更改爲tRow1。 –

+0

至於例外情況,您必須仔細檢查連接在BU中的ID和查找的值,並確保它們是有效的。將它們更改爲普通文本值不會在頁面上產生任何異常。 –