baskaran chellasamy

baskaran chellasamy

  • NA
  • 114
  • 118.9k

Multiple controls with the same ID 'cb1' were found error

Sep 24 2013 7:47 AM

I bind datatable values to gridview. I have added three templatefield column in aspx page for permenantly and I add two dynamic checkbox column by creating column using itemplate interface.everythings ok upto databind.but i want to bind checkbox value from datatable value. if datatable value is true , then checkbox value is checked. For that i tried following code.

DataTable bindclassattendance = inter.getstudentattendresult(comp);

            Createcolumn();


            if (bindclassattendance.Rows.Count>0 )
            {
            Classattendancegrid.DataSource = bindclassattendance;
                Classattendancegrid.DataBind();


                for (int i = 0; i < bindclassattendance.Rows.Count; i++)
                {

                    bool check1 = Convert.ToBoolean(bindclassattendance.Rows[i]["AM"].ToString());
                    if (check1)
                    {
                        CheckBox chk = Classattendancegrid.Rows[i].FindControl("AM") as CheckBox;
                        chk.Checked = true;

                    }
                    else
                    {
                        CheckBox chk = Classattendancegrid.Rows[i].FindControl("AM") as CheckBox;
                        chk.Checked = false;

                    }
                    bool check2 = Convert.ToBoolean(bindclassattendance.Rows[i]["PM"].ToString());
                    if (check2)
                    {
                        CheckBox chk = Classattendancegrid.Rows[i].FindControl("PM") as CheckBox;
                        chk.Checked = true;

                    }
                    else
                    {
                        CheckBox chk = Classattendancegrid.Rows[i].FindControl("PM") as CheckBox;
                        chk.Checked = false;

                    }

                }

and dynamic column Createcolumn() is:

private void Createcolumn()
        {

            TemplateField amtemp = new TemplateField();
            amtemp.ShowHeader = true;
            amtemp.HeaderText = "AM";
            amtemp.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "AM", "AM", "CheckBox");
            Classattendancegrid.Columns.Add(amtemp);

            TemplateField pmtemp = new TemplateField();
            pmtemp.ShowHeader = true;
            pmtemp.HeaderText = "PM";
            pmtemp.ItemTemplate = new gridviewtemplate(DataControlRowType.DataRow, "PM", "PM", "CheckBox");
            Classattendancegrid.Columns.Add(pmtemp);


        }

and the above error is occured. how can solve it and i never used any name as 'cb1'.