【问题标题】:Iterating through TextBoxes in asp.net - why is this not working?遍历 asp.net 中的 TextBoxes - 为什么这不起作用?
【发布时间】:2023-03-20 01:43:01
【问题描述】:

我有 2 种方法尝试遍历我在 asp.net 页面中的所有文本框。第一个正在工作,但第二个没有返回任何东西。有人可以向我解释为什么第二个不起作用吗?

这工作正常:

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);
            }
        }
    }

和“不工作”的代码:

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

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

【问题讨论】:

  • 在第二批代码中,控件是否包含任何内容?

标签: c# asp.net textbox


【解决方案1】:

您的第一个示例是执行一个级别的递归,因此您获得的 TextBoxes 在控件树的深处有多个控件。第二个示例仅获取顶级文本框(您可能很少或没有)。

这里的关键是 Controls 集合并不是页面上的每个控件 - 相反,它只是当前控件的 直接子控件Page 是一种类型Control)。 那些控件可能又拥有自己的子控件。要了解更多信息,请阅读ASP.NET Control Tree hereNamingContainers here。要真正获取页面上任何位置的每个 TextBox,您需要一个递归方法,如下所示:

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;
}

用作extension method,像这样:

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

【讨论】:

  • 好的,但是顶级文本框是什么意思?我的文本框只是表单中的“普通”文本框..
  • @Svein 查看我更新的答案。您的 Web 表单是一个控件树 - 包含控件的控件包含控件,依此类推。 “顶层”是指位于层次结构的最顶层或最外层的控件。
  • 非常感谢您的详细解释和代码示例!我喜欢 Stackoverflow,有很多很棒的人想帮助别人 :-)
  • 这里是另一个扩展方法的链接,该方法将一个名为 All 的方法全部指向控件集合,该方法以递归方式为您提供控件集合中包含的所有控件的可枚举列表。 extensionmethod.net/Details.aspx?ID=5 有时我觉得它很方便。
  • 对不起,我的意思是“添加方法”而不是“所有方法”:)
【解决方案2】:

你需要递归。控件采用树形结构 - Page.Controls 不是页面上所有控件的扁平列表。您需要执行以下操作来获取 TextBoxes 的 all 值:

void GetTextBoxValues(Control c, List<string> strings)
{
    TextBox t = c as TextBox;
    if (t != null)
        strings.Add(t.Text);

    foreach(Control child in c.Controls)
        GetTextBoxValues(child, strings);
}

...

List<string> strings = new List<string>();
GetTextBoxValues(Page, strings);

【讨论】:

    【解决方案3】:

    您可以尝试这段代码来获取所有文本框的列表

    public partial class _Default : System.Web.UI.Page
       {
    
           public List<TextBox> ListOfTextBoxes = new List<TextBox>();
           protected void Page_Load(object sender, EventArgs e)
           {
               // after execution this line
               FindTextBoxes(Page, ListOfTextBoxes);
               //ListOfTextBoxes will be populated with all text boxes with in the page.
    
           }
    
    
           private void FindTextBoxes(Control Parent, List<TextBox> ListOfTextBoxes)
           {
               foreach (Control c in Parent.Controls) {
                   // if c is a parent control like panel
                   if (c.HasControls())
                   {
                       // search all control inside the panel
                       FindTextBoxes(c, ListOfTextBoxes);
                   }
                   else {
                       if (c is TextBox)
                       {
                           // if c is type of textbox then put it into the list
                           ListOfTextBoxes.Add(c as TextBox);
                       }
                   }
               }
           }
       }
    

    【讨论】:

    • 我之前已经通知过您关于链接到您自己的网站而没有署名的问题。
    猜你喜欢
    • 2021-09-17
    • 2019-11-02
    • 1970-01-01
    • 2012-11-17
    • 2019-07-09
    • 2012-09-27
    • 2016-04-18
    相关资源
    最近更新 更多