如何在GridView的特定单元格内获取所有控件 [英] How to get all the controls inside a specific cell in a 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屋!