如何获得动态创建的控件(即动态控件的子控件)的特定嵌套控件? FindControl()方法不起作用,因为它只处理我相信的TopLevel动态控件。回传动态嵌套控件的FindControl()方法
1
A
回答
3
您需要通过您的控件递归的:(C#代码)
public static Control FindControl(Control parentControl, string fieldName)
{
if (parentControl != null && parentControl.HasControls())
{
Control c = parentControl.FindControl(fieldName);
if (c != null)
{
return c;
}
// if arrived here, then not found on this level, so search deeper
// loop through collection
foreach (Control ctrl in parentControl.Controls)
{
// any child controls?
if (ctrl.HasControls())
{
// try and find there
Control c2 = FindControl(ctrl, fieldName);
if (c2 != null)
{
return c2; // found it!
}
}
}
}
return null; // found nothing (in this branch)
}
0
这是我在过去的时代所使用的扩展方法。我发现使用它作为扩展方法会使代码更具表现力,但这只是一种偏好。
/// <summary>
/// Extension method that will recursively search the control's children for a control with the given ID.
/// </summary>
/// <param name="parent">The control who's children should be searched</param>
/// <param name="controlID">The ID of the control to find</param>
/// <returns></returns>
public static Control FindControlRecursive(this Control parent, string controlID)
{
if (!String.IsNullOrEmpty(parent.ClientID) && parent.ClientID.Equals(controlID)) return parent;
System.Web.UI.Control control = null;
foreach (System.Web.UI.Control c in parent.Controls)
{
control = c.FindControlRecursive(controlID);
if (control != null)
break;
}
return control;
}
相关问题
- 1. UserControl中的嵌套控件的FindControl返回null
- 2. 嵌套的主页和.FindControl
- 3. FindControl无法找到我的动态添加的控件
- 4. asp.net FindControl方法返回NULL
- 5. DotNetNuke嵌套控件重定向方法
- 6. FindControl返回错误的控件?
- 7. 在asp.net中处理动态控制(findcontrol)
- 8. ASP.NET - 嵌套主页中的FindControl
- 9. 动态添加控件消失回传
- 10. Bootstrap嵌套传送带主动控制
- 11. FindControl方法无法找到该控件的页
- 12. 无法加载视图状态 - ASP.NET中使用嵌套GridView的动态控件
- 13. 动态控制和回传
- 14. 麻烦与的FindControl和动态地创建的控件
- 15. 动态嵌套形式控制角2
- 16. 动态插入嵌套控制器
- 17. 嵌套本地类的静态方法
- 18. 嵌套类的接口静态方法
- 19. Repeater上的Findcontrol无法找到控件
- 20. 模拟传统的静态方法与嵌套调用
- 21. FindControl不适用于动态创建的用户控件
- 22. 在动态生成的控件上使用FindControl
- 23. FindControl不能与动态创建的控件一起工作
- 24. 如何使用FindControl函数查找动态生成的控件?
- 25. findcontrol没有找到动态添加的控件,它只是在
- 26. Spring嵌套的@Transactional方法和回滚
- 27. 动态的FindControl在JavaScript
- 28. asp.net FindControl方法返回NULL AsyncPostBackTrigger
- 29. 嵌套方法
- 30. 动态创建的控件中的可视Web部件动态控件findcontrol不起作用
thanx。这适用于fieldName参数中的[ClientID]和[ID]。 – TroyS