コードからテーブルの行を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
、それは
tRow
とtRow1
が最高の変数名ではありません動作するはずのように見える:それは誤りが含まれているかのように感謝
私は質問の完全なコードを更新しました。それでもSys.WebForms.PageRequestManagerServerErrorExceptionとしてエラーが発生しました:予期しないエラーが発生しました – venkat14
私は正しいとは思わない内部ループに 'tRow'が表示されます。私はあなたがそれが属していないところでそれを使うかどうかを見ることができるように、 'tRow'の名前をもっと明確にするべきだと思います。 – Adam
それをtRow1にアップデートしました。しかし、まだ同じ問題 – venkat14