您需要通过创建公共属性或函数来公开用户控件功能,以使其执行您需要的操作或按照您的意愿行事。因此,例如,在您的情况下,您可以在用户控件中拥有一个属性,例如(您也可以执行一个函数):
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;
关键是在您的用户控件中公开您想要的内容,因此无论使用什么都不需要知道它的细节或实现。您应该能够像使用任何其他控件一样使用它。