C# 如何获取动态复选框控件id

C# 如何获取动态复选框控件id,c#,asp.net,C#,Asp.net,我正在动态生成复选框,所有这些都写在页面加载中,当我尝试选中复选框时,它显示错误 错误行: 复选框cb=CheckBoxPage.FindControl+j 错误:找到多个具有相同ID“1”的控件。FindControl要求控件具有唯一的ID 我的要求是: 如果我选中复选框,我想计算选中的复选框数量,单选按钮将显示相对选中的复选框 代码如下: string strfromdt = Session["leavefrm"].ToString(); DateTime sta

我正在动态生成复选框,所有这些都写在页面加载中,当我尝试选中复选框时,它显示错误

错误行:

复选框cb=CheckBoxPage.FindControl+j

错误:找到多个具有相同ID“1”的控件。FindControl要求控件具有唯一的ID

我的要求是:

如果我选中复选框,我想计算选中的复选框数量,单选按钮将显示相对选中的复选框

代码如下:

      string strfromdt = Session["leavefrm"].ToString();
        DateTime startDate = Convert.ToDateTime(strfromdt);
        string strtodt = Session["leaveto"].ToString();
        DateTime endDate = Convert.ToDateTime(strtodt);

        string strdays = Session["noofdays"].ToString();
        float daysf = float.Parse(strdays);
        float days = (float)Math.Ceiling(daysf);
        CheckBox chk;
        Label lbl;
        RadioButton rd;

        days++;

                OleDbCommand cmd;
                DbConnection.Open();
                cmd = new OleDbCommand("select HOL_DATE from IND_HOLIDAYS", DbConnection);
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);


                for (int j = 1; j <= days - 1; j++)
                {
                    while(startDate <= endDate)
                    {
                        for (int i = 0; i <= dt.Rows.Count - 1; i++)
                        {
                            string strdate = dt.Rows[i]["HOL_DATE"].ToString();
                            DateTime date = Convert.ToDateTime(strdate);

                            if (startDate == date)

                                startDate = startDate.AddDays(1);
                        }

                        if ((startDate.DayOfWeek == DayOfWeek.Saturday) || ((startDate.DayOfWeek == DayOfWeek.Sunday)))
                        {
                            startDate = startDate.AddDays(1);
                            continue;
                        }
                        break;
                    }


                    chk = new CheckBox();
                    chk.ID = j.ToString();
                    chk.AutoPostBack = true;
                    // chk.Checked = true;
                    lbl = new Label();
                    lbl.Text = startDate.ToString("dd/MM/yyyy");
                    lbl.ID = j.ToString();
                    PlaceHolder1.Controls.Add(lbl);
                    PlaceHolder1.Controls.Add(chk);

                    PlaceHolder1.Controls.Add(new RadioButton { });

                    PlaceHolder1.Controls.Add(new LiteralControl("<BR>"));

                    startDate = startDate.AddDays(1);



                    CheckBox cb = (CheckBox)Page.FindControl("chk" + j);

                   //chk.Checked = CheckBox1Checked;
                   //chk.oncheckedchanged += CheckBox1OnChecked;

                    int chkcount = 0;
                    if (chk.Checked)
                    {
                        chkcount++;
                    }
                    int chkcount1 = chkcount;
                }

您已经为chk和lbl提供了相同的ID,它们都是j.ToString,您需要使它们唯一:

chk.ID = string.Format("chk{0}", j);
lbl.ID = string.Format("lbl{0}", j);
// Now you can FindControl:
CheckBox cb = (CheckBox)Page.FindControl("chk" + j);

您需要在OnInit方法中添加动态控件,以便这些控件在所有情况下都能正常工作。我记得在页面加载中添加动态控件时遇到问题

RGraham已经指出了一个错误,将相同的ID添加到不同的控件中

还有,这句话似乎没有任何意义


占位符1.Controls.Addnew单选按钮{}

我得到了复选框id,但即使我选中了复选框,也显示了复选框id为False我想j是唯一的,它将创建多达1天的具有唯一id的复选框和标签,因为它位于for循环中。@Anandaraj如果只使用一次,它将是唯一的。在这种情况下,它在同一个循环中使用了两次。我正在获取复选框id,但即使我选中了复选框,id也显示为选中false@RGraham,是的,我没有看到复选框和标签使用相同的j.ToString值。你的回答是正确的。