通过asp.net文本框迭代 - 这是为什么不工作? [英] Iterating through TextBoxes in asp.net - why is this not working?

查看:117
本文介绍了通过asp.net文本框迭代 - 这是为什么不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个方法,我想通过我的一个asp.net页面中的所有文本框进行迭代。首先是工作,但第二个不返回任何东西。为什么第二个不工作对我来说可能有人请解释一下吗?

该工程确定:

 列表<串GT;名单=新名单,LT;串>();    的foreach(在Page.Controls控制C)
    {
        的foreach(在c.Controls控制childc)
        {
            如果(childc是文本框)
            {
                list.Add(((文本框)childc)。文本);
            }
        }
    }

和不工作code:

 列表<串GT;名单=新名单,LT;串>();    的foreach(在控制控制控制)
    {
        文本框的textBox =控制,文本框;
        如果(textBox中!= NULL)
        {
            list.Add(textBox.Text);
        }
    }


解决方案

您第一个例子是做递归的一个水平,所以你得到了一个以上的控制深的控件树文本框。第二个例子只得到顶级文本框(你可能很少或无)。

这里的关键是,控件收集不是每个页面上的控制 - 更确切地说,它只是其中的直接子控件电流控制(和是一种控制)的。的那些的控制又可能有自己的子控件。要了解更多关于这一点,阅读有关 ASP.NET控件树这里以及有关的此处 NamingContainers。要真正让每一个文本框的页面上的任何地方,你需要一个递归方法,如:

 公共静态的IEnumerable< T> FindControls< T>(这个调节控制,布尔递归),其中T:控制
{
    清单< T>找到=新名单< T>();
    动作<控制>搜索= NULL;
    搜索= CTRL =>
        {
            的foreach(在ctrl.Controls控制子)
            {
                如果(typeof运算(T).IsAssignableFrom(child.GetType()))
                {
                    found.Add((T)的孩子);
                }
                如果(递归)
                {
                    搜索(小孩);
                }
            }
        };
    搜索(控制);
    返回找到;
}

这是作为一个扩展方法,像这样:

  VAR allTextBoxes = this.Page.FindControls<&文本框GT;(真);

I have 2 methods I tried to iterate through all my textboxes in an asp.net page. The first is working, but the second one is not returning anything. Could someone please explain to me why the second one is not working?

This works ok:

List<string> list = new List<string>();

    foreach (Control c in Page.Controls)
    {
        foreach (Control childc in c.Controls)
        {
            if (childc is TextBox)
            {
                list.Add(((TextBox)childc).Text);
            }
        }
    }

and the "not working" code:

List<string> list = new List<string>();

    foreach (Control control in Controls)
    {
        TextBox textBox = control as TextBox;
        if (textBox != null)
        {
            list.Add(textBox.Text);
        }
    }

解决方案

Your first example is doing one level of recursion, so you're getting TextBoxes that are more than one control deep in the control tree. The second example only gets top-level TextBoxes (which you likely have few or none).

The key here is that the Controls collection is not every control on the page - rather, it is only the immediate child controls of the current control (and a Page is a type of Control). Those controls may in turn have child controls of their own. To learn more about this, read about the ASP.NET Control Tree here and about NamingContainers here. To truly get every TextBox anywhere on the page, you need a recursive method, like this:

public static IEnumerable<T> FindControls<T>(this Control control, bool recurse) where T : Control
{
    List<T> found = new List<T>();
    Action<Control> search = null;
    search = ctrl =>
        {
            foreach (Control child in ctrl.Controls)
            {
                if (typeof(T).IsAssignableFrom(child.GetType()))
                {
                    found.Add((T)child);
                }
                if (recurse)
                {
                    search(child);
                }
            }
        };
    search(control);
    return found;
}

Which is used as an extension method, like so:

var allTextBoxes = this.Page.FindControls<TextBox>(true);

这篇关于通过asp.net文本框迭代 - 这是为什么不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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