选择“动态”表中的下拉列表项

发布于 2024-11-14 18:37:01 字数 1059 浏览 4 评论 0原文

//Code BehindFile

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/>");
         }

问题出在下拉列表项的选择上。我无法打印相应的选定项值。有人可以帮忙吗?

//Code BehindFile

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/>");
         }

Problem is with the Selection of dropdownlist items.I could not print the corresponding selected item values.could anyone help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2024-11-21 18:37:01

当您位于最后一个嵌套的 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+" ");
 }

Check tc.Controls[2] when you are in the last nested foreach. Is it possible that your dropdown list is something other than the third control?

I don't see any reason that would force it to be the third control in that Cell.

You would probably be better off doing something like this:

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

instead of:

 if (tc.Controls[2] is DropDownList)
 {
    Response.Write(((DropDownList)tc.Controls[0]).SelectedItem.Text+" ");
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文