我成功创建了用于显示错误消息的用户控件。现在一切正常,但显示消息时可以访问后台控件。如何禁用页面控件或页面点击或选择任何控件。当消息面板关闭时,它应该启用页面控件。如何在选择usercontrol时禁用ASP.NET页面中的控件?
我找到了答案的朋友。
void DisableControls(Control parent, bool status)
{
foreach (Control c in parent.Controls)
{
if (c is DropDownList)
{
((DropDownList)(c)).Enabled = status;
}
if (c is Button)
{
((Button)(c)).Enabled = status;
}
if (c is TextBox)
{
((TextBox)c).Enabled = status;
}
if (c is RadioButton)
{
((RadioButton)c).Enabled = status;
}
if (c is ImageButton)
{
((ImageButton)c).Enabled = status;
}
if (c is CheckBox)
{
((CheckBox)c).Enabled = status;
}
if (c is DropDownList)
{
((DropDownList)c).Enabled = status;
}
if (c is HyperLink)
{
((HyperLink)c).Enabled = status;
}
if (c is GridView)
{
((GridView)c).Enabled = status;
}
if (c is Table)
{
((Table)c).Enabled = status;
}
if (c is Menu)
{
((Menu)c).Enabled = status;
}
if (c is TreeView)
{
((TreeView)c).Enabled = status;
}
}
}
当用户控件被激活时,我得到了div中我添加了controls.but的页面中的控件从我调用的地方也是活动的。我想禁用该页面中的控件。 – 2010-12-11 14:36:45
我想你会发现你可以通过简单地使用下面的代码来大大简化你的代码*:foreach(parent.Controls中的Control c){c.Enabled = false; }因为Enabled是Control的一个属性。 – Crisfole 2010-12-20 14:43:42
感谢您的回复。我已经尝试过,但不能成功完成。它会抛出一个错误。 – 2010-12-20 15:19:25