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.

3 Answers 3

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";
        }
Sign up to request clarification or add additional context in comments.

2 Comments

how do I check for the index of the row here? I need to get this to be able to determine which checkbox gets checked and which not...
Your event is being fired?
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" ...

1 Comment

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

2 Comments

ok when I do it like this, the event is being fired but I always get the same ID "ctl00"...
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.