2017-10-05 22 views
1

コードからテーブルの行をasp.netテーブルに追加しようとしています。私は以下のコードを試しました。変更されたコードを追加しました。しかし、 "Sys.WebForms.PageRequestManagerServerErrorExceptionとしてエラーを取得する:予期しないエラーが発生しました。"動的に5 tablerowを追加する

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 

どのように動的にテーブルの行を追加しますか?ループにtRow1

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

変更tRow、それは

tRowtRow1が最高の変数名ではありません動作するはずのように見える:それは誤りが含まれているかのように感謝

答えて

0

次のコードが見えます。それはあなたの問題の究極の源である可能性が最も高いです、より記述的な名前を選んだ!

+0

私は質問の完全なコードを更新しました。それでもSys.WebForms.PageRequestManagerServerErrorExceptionとしてエラーが発生しました:予期しないエラーが発生しました – venkat14

+0

私は正しいとは思わない内部ループに 'tRow'が表示されます。私はあなたがそれが属していないところでそれを使うかどうかを見ることができるように、 'tRow'の名前をもっと明確にするべきだと思います。 – Adam

+0

それをtRow1にアップデートしました。しかし、まだ同じ問題 – venkat14

1

@Adamに記載されているtypo以外に、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

例外として、IDと連結してBUとルックアップした値を再度チェックし、それらが有効であることを確認する必要があります。これらを通常のテキスト値に変更しても、ページに例外は発生しません。 –

関連する問題