且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在GridView的特定单元格内获取所有控件

更新时间:2023-11-03 09:16:10

您可以使用(手写代码)获得复选框:

  foreach(grdApproverDetails.Rows中的GridViewRow行){for(int k = 0; k< row.Cells.Count; k ++){for(int i = 0; i< row.Cells [k] .Controls.Count; i ++){控件控件= row.Cells [k] .Controls [i];如果(控件是CheckBox){CheckBox chk =将控件作为CheckBox;if(chk!= null& chch.Checked)//...}}}} 

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?

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)
               //...
           }
       }
    }
}