我有一个复杂的asp.net表单,在一个表单中有50到60个字段,就像有Multiview个一样,在MultiView中有GridView个字段,在GridView中有CheckBoxes个字段.

目前,我正在使用FindControl()方法的链接并检索子ID.

现在,我的问题是,有没有其他方法/解决方案可以在ASP中找到嵌套控件.网

推荐答案

如果您正在寻找特定类型的控件,您可以使用像这样的递归循环- http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx

下面是我创建的一个示例,它返回给定类型的所有控件

/// <summary>
/// Finds all controls of type T stores them in FoundControls
/// </summary>
/// <typeparam name="T"></typeparam>
private class ControlFinder<T> where T : Control 
{
    private readonly List<T> _foundControls = new List<T>();
    public IEnumerable<T> FoundControls
    {
        get { return _foundControls; }
    }    

    public void FindChildControlsRecursive(Control control)
    {
        foreach (Control childControl in control.Controls)
        {
            if (childControl.GetType() == typeof(T))
            {
                _foundControls.Add((T)childControl);
            }
            else
            {
                FindChildControlsRecursive(childControl);
            }
        }
    }
}

Asp.net相关问答推荐

授权错误错误 400:C# 上的 redirect_uri_mismatch

RNGCryptoServiceProvider - 随机数审核

ASP.NET 核心中的 Request.CreateResponse

IIS 将旧用户名返回到我的应用程序

正在检索组件的 COM 类工厂......错误:80070005 访问被拒绝. (来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))

从 IIS 7/8 中的静态内容中删除服务器标头

带有邮箱地址参数的 WebApi 方法从 HttpClient 返回 404

为应用程序池Classic .NET AppPool提供服务的进程与 Windows 进程激活服务发生了致命的通信错误

oAuth ASP.NET 成员资格提供程序

如何在 C# 应用程序中调用 VBScript 文件?

如何获得 System.Diagnostics.Process 的输出?

禁用 web.config 继承?

如何保护存储在 web.config 中的密码?

Asp.Net web 服务:我想返回错误 403 禁止

将 MemoryStream 写入响应对象

主机与 DnsSafeHost

简单的 LINQ 和列表错误:WhereListIterator`1[Task]' to type 'System.Collections.Generic.List`1[Task]'

以编程方式滚动到锚标记

中继器中的中继器

使用 FileUpload Control 获取文件的完整路径