【发布时间】:2008-11-24 15:25:11
【问题描述】:
我有一个母版页和一个内容页
我的内容页面有许多控件,- 下拉列表、文本框、单选按钮。
我想遍历所有控件,如果它是上面的控件之一,那么我想获取选定的值、文本等
我知道我可以通过名称直接访问控件,但由于这是我在 asp.net 上的学习经历,我希望能够浏览控件集合
我试过了
foreach(Controls ctrl in Master.Controls)
{
}
但我无法使用所需的控件。
编辑:: 我想我拔枪的速度有点太快了,因为我的智能感知一直在我身上失败。我在内容页面上的代码中解决了我的问题
protected void btnSearch_Click(object sender, EventArgs e)
{
foreach (Control ctrl in Master.Controls)
{
if (ctrl is HtmlForm)
{
foreach(Control formControl in ctrl.Controls)
{
if (formControl is ContentPlaceHolder)
{
foreach (Control placeHolderControl in formControl.Controls)
{
if (placeHolderControl is DropDownList)
{
string test = "AA";
}
}
}
}
}
}
}
现在构建一个递归例程
【问题讨论】: