How to get all the controls inside a specific cell in a GridView

本秂侑毒 提交于 2021-01-28 00:16:07

问题


I am generating CheckBox controls dynamically inside a GridView. Now i need to validate if atleast one CheckBox is selected and also while saving data i need to iterate through all the controls inside the cell.

Now the issue is i cannot do grdApproverDetails.Rows[i].FindControl('controlID'), because the ID's are dynamically generated based on the control count. As shown in this thread.

This is how the GridView looks and Approver Name is the column inside which i need to find controls, if CheckBoxes.

How can i get all the controls inside a GridView cell and iterate through?


回答1:


You can get checkboxes using (handwritten code):

foreach (GridViewRow row in grdApproverDetails.Rows)
{
    for (int k = 0; k < row.Cells.Count; k++)
    {
       for (int i = 0; i < row.Cells[k].Controls.Count; i++)
       {
           Control control = row.Cells[k].Controls[i];
           if(control is CheckBox)
           {
               CheckBox chk = control as CheckBox;
               if(chk != null && chk.Checked)
               //...
           }
       }
    }
}



回答2:


I got it working thanks to Emanuele

foreach (GridViewRow row in grdApproverDetails.Rows)
{
     List<CheckBox> listCkb = new List<CheckBox>();

     ControlCollection cntrColl=  row.Cells[2].Controls;
     foreach (Control cntr in cntrColl)
     {
         if (cntr is CheckBox && cntr.ID.Contains("approvernamesdynamic_"))
         {

         }
      }
}


来源:https://stackoverflow.com/questions/45409764/how-to-get-all-the-controls-inside-a-specific-cell-in-a-gridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!