2010-08-30 36 views
1

我有一個頁面,其中包含一個表格,其中有幾行,在該行中有複選框。ASP.Net中的循環槽控件C#

現在我想要的東西是循環槽的所有複選框,看看他們是否被檢查或不。

這是我目前的做法:

foreach (Control c in Page.Controls) 
{ 
    if(c is Checkbox){ 
    } 
} 

現在的問題是,我只接收2控制,頁面和表。所以複選框在: 表 - > TableRow - > TableCell - >複選框

有沒有辦法讓一個頁面上的所有控件,而不是爲了擺脫控制?

提前致謝!

回答

1

control.Controls將僅返回第一級子控件。有關詳細信息,請檢查this question

0

我只是做了一個嵌套的foreach循環是這樣的:

List<Control> allControls = new List<Control>(); 
      List<string> selectedIDs = new List<string>(); 

     foreach (Control c in this.pnlTable.Controls) 
     { 
      allControls.Add(c); 

      if (c.Controls.Count > 0) 
      { 
       foreach (Control childControl in c.Controls) 
       { 
        allControls.Add(childControl); 

        if (childControl.Controls.Count > 0) 
        { 
         foreach (Control childControl2 in childControl.Controls) 
         { 
          allControls.Add(childControl2); 

          if (childControl2.Controls.Count > 0) 
          { 
           foreach (Control childControl3 in childControl2.Controls) 
           { 
            allControls.Add(childControl3); 
           } 
          } 
         } 
        } 
       } 
      } 
     } 

     foreach (Control control in allControls) 
     { 
      if (control is CheckBox) 
      { 
       if (((CheckBox)(control)).Checked) 
       { 
        selectedIDs.Add(((CheckBox)(control)).ID); 
       } 
      } 
     } 

取決於是否和foreach我增加了一個控制的深度..

希望這可以幫助其他人用同樣的問題...