我需要更改我的 aspx 页面中许多控件的可见性。
我找到了几种方法来获得像这样的控件
但是我不能为这个控件设置值,因为它们是由 val 传递的,而且我不知道在这种情况下如何添加 ref 关键字。
试试这个链接,它应该可以正常工作。顺便说一句,控件是引用类型而不是值类型。
从您问题中的示例中,执行以下操作:
IEnumerable<Control> EnumerateControlsRecursive(Control parent)
{
foreach (Control child in parent.Controls)
{
yield return child;
foreach (Control descendant in EnumerateControlsRecursive(child))
yield return descendant;
}
}
用法:
foreach (Control c in EnumerateControlsRecursive(Page))
{
if(c is TextBox)
{
var theTextBox = c as TextBox;
theTextBox.Visible = false;
}
if(c is Label)
{
var theLabel = c as Label;
theLabel.Visible = false;
}
...
}