首页 > 解决方案 > FindControl in Gridview is empty with Framework 4.5.2, with Framework 4 is working

问题描述

I add checkboxes on RowDataBound in a Gridview:

 for (int i = 1; i < e.Row.Cells.Count; i++)
            {
                CheckBox cb = new CheckBox();
                cb.ID = "Checkbox" + i.ToString();
                if (e.Row.Cells[i].Text == "true") cb.Checked = true; else cb.Checked = false;
                e.Row.Cells[i].Controls.Add(cb);
            }

On Button click I want to read the state of the checkboxes:

foreach (GridViewRow gvr in GV.Rows)
        {
                for (int i = 1; i < GV.HeaderRow.Cells.Count; i++)
                {
                    CheckBox cb = gvr.FindControl("Checkbox" + i.ToString()) as CheckBox;
                    if (cb.Checked == true)
                    {
                      //To something
                    }
                 }
         }

This code works with Framework 4 (controls are found) but not with Framework 4.5.2 (controls are empty). How can I fix this? Thanks

标签: asp.netfindcontrol

解决方案


推荐阅读