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]。您的下拉列表是否可能不是第三个控件?

我看不出有任何理由迫使它成为该单元中的第三个控件。

你可能会更好地做这样的事情:

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 回答