2017-10-05 49 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和查找的值,并确保它们是有效的。将它们更改为普通文本值不会在页面上产生任何异常。 –