2010-11-03 57 views
7

我有2個方法我嘗試遍歷所有我的文本框在一個asp.net頁面。第一個工作,但第二個沒有返回任何東西。有人可以向我解釋爲什麼第二個不工作?在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); 
     } 
    } 
+0

在第二批代碼中,控件是否包含任何東西? – brumScouse 2010-11-03 22:05:44

回答

9

你的第一個例子是做遞歸的一個水平,所以你得到超過文本框在控制樹中深入一個控制。第二個示例僅獲取頂級TextBoxes(您可能很少或沒有)。

的這裏關鍵是Controls收集不是每個頁面上的控制 - 更確切地說,它只是直接子控制電流控制的(和Page是一種類型的Control)。 那些控件可能依次擁有自己的子控件。要詳細瞭解此信息,請閱讀ASP.NET Control Tree here和約NamingContainers here。要真正讓每一個文本框的頁面上的任何地方,你需要一個遞歸方法,像這樣:

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); 
+0

好吧,但是頂級TextBoxes是什麼意思?我的文本框只是「普通」文本框的形式.. – 2010-11-03 22:05:12

+0

@Svein看到我更新的答案。您的Web表單是一個控件樹 - 包含包含控件的控件的控件等等。 「頂級」意味着生活在層次最頂層或最外層的控制。 – 2010-11-03 22:10:47

+0

非常感謝您的詳細解釋和代碼示例!我喜歡Stackoverflow,那麼多想要幫助其他人的偉大人物:-) – 2010-11-03 22:15:24

1

你需要遞歸。這些控件是樹形結構 - Page.Controls不是頁面上所有控件的展開列表。你需要做類似下面的東西拿到文本框的所有值:

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

你可以試試這段代碼來獲取所有文本框

名單
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); 
        } 
       } 
      } 
     } 
    } 
+0

我之前已通知您關於鏈接到您自己的網站而沒有署名。 – 2013-03-05 06:46:53