如何在GridView的特定单元格内获取所有控件 [英] How to get all the controls inside a specific cell in a GridView

查看:76
本文介绍了如何在GridView的特定单元格内获取所有控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

现在的问题是我无法执行 grdApproverDetails.Rows [i] .FindControl('controlID'),因为ID是根据控件计数动态生成的.如

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

解决方案

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

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

这篇关于如何在GridView的特定单元格内获取所有控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆