2011-08-18 54 views

回答

1

你可以嘗試在這些線路上的東西,如果所有的文本框在頁面上直接

foreach(Control c in Page.Controls) 
{ 
    if (c is TextBox) 
    { 
     //get the text 
    } 
} 

這會不會對孩子的控制工作,爲您將不得不遞歸迭代

+0

我會改變,所以你使用文本框TB = C爲文本框,然後檢查是否TB!= NULL,可以使確定它是一個TextBox。否則,您必須將控件取消兩次。 –

1

是的,你可以把你的控制面板,然後迭代和獲得價值。例如

foreach (Control ctrl in Panel1.Controls) 
    { 
     if (ctrl.GetType().Name == "TextBox") 
     { 
      if (((TextBox)ctrl).Text != string.Empty) 
      { 
       // do stuff here 
      } 
     } 
    } 
11
public IEnumerable<string> AllTextsFromTextboxes() 
{ 
    foreach (var control in Page.Controls.OfType<TextBox>()) 
    { 
     yield return control.Text;  
    } 
} 
1
private void FindSelecedControl(Control control) 
{ 
    if (control is TextBox) 
    { 
     TextBox txt = (TextBox)control; 
     txt.Enabled = false; 
    } 
    else 
    { 
     for (int i = 0; i < control.Controls.Count; i++) 
     { 
      FindSelecedControl(control.Controls[i]); 
     } 
    } 
} 

foreach (Control control1 in this.Form.Controls) 
{ 
    FindSelecedControl(control1); 
} 
相關問題