简体   繁体   English

如何通过选中获取动态复选框控件ID为true?

[英]How to get Dynamic checkbox control id's by checked is true?

I am dynamically generating checkboxes, all this wrote in page_load 我正在动态生成复选框,所有这些都写在page_load中

My requirement is: 我的要求是:

if i check the checkboxes i want count of the number of checkboxes checked and radio buttons will appeared relative checked checkboxes. 如果我选中复选框,我想计算选中的复选框的数量,单选按钮将显示相对选中的复选框。

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

even i checked the checkbox the cd shows checked = false 即使我选中了cd,它也显示出checked = false

below is the code: 下面是代码:

    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;
            }

You're searching for a non-existing ID. 您正在搜索不存在的ID。

First you're setting the Id of the control to just a number (j) and later you'll try to find it with with a search to "chk"+the number j. 首先,将控件的ID设置为仅一个数字(j),然后尝试通过搜索“ chk” +数字j来查找它。

2 options here: 这里有2个选项:

Change chk.ID = j.ToString(); 更改chk.ID = j.ToString(); to chk.ID ="chk" + j.ToString(); chk.ID ="chk" + j.ToString();

or 要么

Change CheckBox cb = (CheckBox)Page.FindControl("chk" + j); 更改CheckBox cb = (CheckBox)Page.FindControl("chk" + j); to CheckBox cb = (CheckBox)Page.FindControl(j); CheckBox cb = (CheckBox)Page.FindControl(j);

Personally I would go for the first option since naming a control with just a number wouldn't be a good idea. 就个人而言,我会选择第一个选项,因为仅用一个数字命名控件不是一个好主意。

UPDATE 更新

for (int j = 1; j <= days - 1; j++)
{
    ...

    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);

    //No need fot this. You still have the object chk from a few lines above
    // CheckBox cb = (CheckBox)Page.FindControl("chk" + j);

    //If you want to use this, put these lines before you add the control.
    //chk.Checked = CheckBox1Checked;
    //chk.oncheckedchanged += CheckBox1OnChecked;

    //You should declare this outside the for-loop or even outside the method 
    //if you want to use it elsewhere
    int chkcount = 0;

    //Here you are using the correct object.
    //chk.Checked should reflect exactly what you've set above.
     if (chk.Checked)
    {
        chkcount++;
    }
    int chkcount1 = chkcount;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM