如何在选择usercontrol时禁用ASP.NET页面中的控件?
我成功创建了用于显示错误消息的用户控件。现在一切正常,但显示消息时可以访问后台控件。如何禁用页面控件或页面点击或选择任何控件。当消息面板关闭时,它应该启用页面控件。如何在选择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;
}
}
}
我明白了,你想让它像一个模态对话框。它可以通过简单的html + javascript完成。 您必须创建一个覆盖整个页面的透明div覆盖图,以便用户不用点击控件就可以点击div。 Z-index指示其余控件的位置。
<!-- Div Overlay -->
<div id="div-overlay" style="position: absolute; height: 100%; width: 100%; z-index: 200; display: none; opacity: 0.0"></div>
<!-- Scripts to show/hide overlay -->
<script type="text/javascript">
function showOverlay() {
var e = document.getElementById('div-overlay');
e.style.display = 'block';
}
function hideOverlay() {
var e = document.getElementById('div-overlay');
e.style.display = 'none';
}
</script>
希望它有帮助。
您可以简单地用一个DIV与CSS的帮助下,你可以能够显示该div就像一个模式弹出或简单地使用jQuery的模态弹出http://jqueryui.com/demos/dialog/或asp.net ajaxcontrol工具http://www.asp.net/ajax/ajaxcontroltoolkit/samples/modalpopup/modalpopup.aspx
不,我不想在这个使用Ajax,因为我有一些像页面闪烁等问题。 – 2010-12-11 14:34:50
如果可以使用JQuery,或者只是简单地使用CSS来显示像模态弹出窗口,然后所有其他控件将不可访问 – 2010-12-11 14:38:35
你能给我一个例子 – 2010-12-11 15:05:45
是你试图创建一个模态对话框?如果是的话,你可以使用asp.net ajax中的ModalPopupExtender控件。检查此链接:
没有我不在其中使用ajax – 2010-12-11 14:33:48
您是否尝试过使用此控件?我不认为你会得到任何问题,如页面闪烁。 – sid 2010-12-11 19:18:13
当页面重新加载或刷新模型弹出窗口时,它会持续几毫秒 – 2010-12-12 05:37:57
当用户控件被激活时,我得到了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