Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何获取GridView中特定单元格内的所有控件_C#_Asp.net_Gridview_Findcontrol - Fatal编程技术网

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

C# 如何获取GridView中特定单元格内的所有控件,c#,asp.net,gridview,findcontrol,C#,Asp.net,Gridview,Findcontrol,我在GridView中动态生成复选框控件。现在,我需要验证是否至少选中了一个复选框,并且在保存数据时,我需要遍历单元格中的所有控件 现在的问题是我无法执行grdApproverDetails.Rows[i].FindControl('controlID'),因为ID是基于控件计数动态生成的。如线程中所示 这就是GridView的外观,Approver Name是我需要在其中查找控件(如果选中)的列。 如何获取GridView单元格中的所有控件并进行迭代 您可以使用(手写代码)获取复选框: fo

我在GridView中动态生成复选框控件。现在,我需要验证是否至少选中了一个复选框,并且在保存数据时,我需要遍历单元格中的所有控件

现在的问题是我无法执行
grdApproverDetails.Rows[i].FindControl('controlID')
,因为ID是基于控件计数动态生成的。如线程中所示

这就是GridView的外观,Approver Name是我需要在其中查找控件(如果选中)的列。


如何获取GridView单元格中的所有控件并进行迭代

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

foreach(grdApproverDetails.Rows中的GridViewRow行)
{
对于(int k=0;k
多亏了伊曼纽尔,我才让它工作起来

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_"))
         {

         }
      }
}
foreach(grdApproverDetails.Rows中的GridViewRow行)
{
List listCkb=新列表();
ControlCollection cntrColl=行。单元格[2]。控件;
foreach(cntrColl中的控制cntr)
{
如果(cntr是复选框&&cntr.ID.Contains(“approvernamesdynamic”))
{
}
}
}

如何创建复选框?在aspx或在RowCreated/RowDatabound事件中?grdApproverDetails.Rows[i]。复选框类型为
CheckBox
的控件可以是解决方案吗?@Emanuele这就是我创建
CheckBox
的方法。这不会返回网格中的任何控件。我编辑了我的帖子。我展示了一个解决方案,然后它取决于表结构。您可以编辑它:
行.单元格
行.单元格[k].控件
,容器控件上的递归等。
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_"))
         {

         }
      }
}