0

I have a GridView which holds user data. When the Page_Load Method is called, I get data using a DataTable and then bind it to the GridView. At the end of each row, I have added a CheckBox. This CB is used as a pointer for which entity the user wants to edit.

My problem is the Check_Changed Event of the CheckBoxes. I do not know how to add a handler if the control is generated programmatically. I also need the index of the row (a field value is also possible, but the column header and the column itself are hidden).

 foreach (GridViewRow gvr in grdMitgliedsliste.Rows)
 {
       //add checkbox for every row
       TableCell cell = new TableCell();
       CheckBox box = new CheckBox();
       cell.Controls.Add(box);
       gvr.Cells.Add(cell);

       //Hide columns for userid, status, etc. 
       gvr.Cells[0].Visible = false;
       gvr.Cells[3].Visible = false;
       gvr.Cells[4].Visible = false;
       gvr.Cells[5].Visible = false;
       gvr.Cells[8].Visible = false;
       gvr.Cells[9].Visible = false;  
 } 

I have already tried implementing the handler from here, but it gives me no index argument so the program cannot determine in which row the checkbox was checked.

LeonidasFett
  • 3,052
  • 4
  • 46
  • 76

3 Answers3

1
   TableCell cell = new TableCell();
   CheckBox box = new CheckBox();
   box.Check += new EventHandler(Checked_Changed);
   cell.Controls.Add(box);
   gvr.Cells.Add(cell);

sorry, im allready about to drive home so its just an fast answer. mabye you have to correct the event after box."event" ...

DatRid
  • 1,169
  • 2
  • 21
  • 46
  • I already tried this but I failed to get the index of the row where the CheckBox sits. I have multiple rows, each one having a checkbox. If one gets checked, the others should get unchecked. – LeonidasFett Jun 03 '13 at 14:19
1
   protected void Page_Load(object sender, EventArgs e)
        {
            List<string> names = new List<string>();
            names.Add("Jhonatas");

            this.GridView1.DataSource = names;
            this.GridView1.DataBind();

            foreach (GridViewRow gvr in GridView1.Rows)
            {
                //add checkbox for every row
                TableCell cell = new TableCell();
                CheckBox box = new CheckBox();
                box.AutoPostBack = true;
                box.ID = gvr.Cells[0].Text;

                box.CheckedChanged += new EventHandler(box_CheckedChanged);
                cell.Controls.Add(box);
                gvr.Cells.Add(cell);
            }
        }

        void box_CheckedChanged(object sender, EventArgs e)
        {
            string test = "ok";
        }
1

You should go this way:

first of all when you are generating the checkbox

       CheckBox box = new CheckBox();
       box.AutoPostBack=true;

provide an id to the checkbox as

       box.ID=Convert.toString(Session["Count"]);

do initialize the "Count" when the page loads in the session. also increment the "Count" everytime you add a new checkbox.

secondly, define the event handler for your dynamic check box like this:

       box.CheckedChange += MyHandler;

and define the MyHandler

       protected void MyHandler(object sender, EventArgs e)
        {
             //Do some stuff
        }

now you may get the id for the checkbox from which the event has been fired inside the MyHandler, which will actually be the row number.

          CheckBox cb = (CheckBox)sender;
          string id = cb.ID;
  • ok when I do it like this, the event is being fired but I always get the same ID "ctl00"... – LeonidasFett Jun 03 '13 at 14:44
  • thanks with a little modification I managed to get it to work. I just added a hidden field which contains the ID of the data set. then I just use that to refer to the checkbox. – LeonidasFett Jun 04 '13 at 13:33