您需要通過製作公共屬性或函數來使其滿足您的需要或行爲,從而暴露用戶控件的功能。因此,例如,你的情況,你可以像你的用戶控件有一個屬性(你也可以做一個函數):
public List<string> SomeValues
{
get
{
// return null if checkbox is not checked, you could just as easily return an empty list.
List<string> lst = null;
if (yourCheckBox.Checked)
{
lst = new List<string>();
// You could have something that iterates and find your controls, remember you
// are running this within your user control so you can access all it's controls.
lst.Add(yourTextBox1.Text);
lst.Add(yourTextBox2.Text);
lst.Add(yourTextBox3.Text);
// etc...
}
return lst;
}
}
然後在你的頁面,您可以訪問您的用戶控制和調用這個屬性來獲取值:
// assuming you defined your usercontrol with the 'yourUserControl' ID
List<string> lst = yourUserControl.SomeValues;
的關鍵是要揭露你想要在你的用戶控件什麼,所以無論是用它並不需要了解它的細節或執行。您應該能夠像使用其他任何控件一樣使用它。
這就是我想念的,謝謝。 – mwright 2010-03-03 16:03:16