在 ASP.NET 中找到控件的更好方法 [英] Better way to find control in ASP.NET

查看:45
本文介绍了在 ASP.NET 中找到控件的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a complex asp.net form,having even 50 to 60 fields in one form like there is Multiview, inside MultiView I have a GridView, and inside GridView I have several CheckBoxes.

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

Currently I am using chaining of the FindControl() method and retrieving the child ID.

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

Now, my question is that is there any other way/solution to find the nested control in ASP.NET.

推荐答案

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

If you're looking for a specific type of control you could use a recursive loop like this one - http://weblogs.asp.net/eporter/archive/2007/02/24/asp-net-findcontrol-recursive-with-generics.aspx

这是我制作的一个示例,它返回给定类型的所有控件

Here's an example I made that returns all controls of the given type

/// <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 中找到控件的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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