0

//コード ビハインド ファイル

public void Button1_Click(object sender, EventArgs e)
 {
             while (reader.Read())
         {

            DropDownList ddl = new DropDownList();
             string[] s = { "Present", "Absent1", "Absent2", "Absent3" };
             for (int i = 0; i < 3; i++)
             {
                 ddl.Items.Add(s[i]);
             }
             ddl.ID = "ddl";
             TableCell c2 = new TableCell();
             c2.Controls.Add(ddl);
             r.Cells.Add(c2);
             Table1.Rows.Add(r);
            }
 }

 public void Button2_Click1(object sender, EventArgs e)

 {

         foreach (TableRow tr in Table1.Controls)
         {
             foreach (TableCell tc in tr.Controls)
             {
                 if (tc.Controls[2] is DropDownList)
                {
                 Response.Write(((DropDownList)tc.Controls[2]).SelectedItem.Text+" ");
                }
             }
             Response.Write("<br/>");
         }

ドロップダウンリスト項目の選択に問題があります。対応する選択された項目の値を印刷できませんでした。誰か助けてもらえますか?

4

1 に答える 1

0

最後のネストされた foreach にいるときに tc.Controls[2] を確認してください。ドロップダウン リストが 3 番目のコントロール以外である可能性はありますか?

そのセルの 3 番目のコントロールにする必要がある理由はわかりません。

おそらく、次のようなことをしたほうがよいでしょう。

if(tc.FindControl("ddl") != null)
{
   Response.Write(((DropDownList)tc.FindControl("ddl")).SelectedItem.Text+" ");
}

それ以外の:

 if (tc.Controls[2] is DropDownList)
 {
    Response.Write(((DropDownList)tc.Controls[0]).SelectedItem.Text+" ");
 }
于 2011-06-07T21:02:22.560 に答える