您的位置:首页 > 其它

禁用页面所有控件

2011-01-11 13:17 148 查看
今天写一个页面,页面内容一条记录的详细信息,我想给加上权限控制,有权限的可以修改,没有权限的把所有控件都给禁用了,只能浏览,网上找了找,找到两段代码,直接用上了。

个人感觉,方案二节省服务器资源,因为是JavaScript,在客户端执行;而方案一对页面文件的大小不会有大的影响,对于程序员开发来说简单快捷,当然,肯定会增加一部分的服务器资源消耗。

方案一:遍历页面的所有控件并禁用。

代码如下:

protected void DisableAllControls()
{
foreach (Control control in form1.Controls)
{
if (control is WebControl)
{
((WebControl)control).Enabled = false;
}
else if (control is HtmlControl)
{
((HtmlControl)control).Disabled = true;
}
}
}


方案二:给页面加上一层半透明的div(转载自:http://hi.baidu.com/liuspring/blog/item/4157564a0ca8952709f7efe2.html

代码如下:

<html>
<head>
<title>半透明div</title>

<mce:style><!--
.#mask {
visibility: hidden;
background-color: #cccccc;
left: 0px;
position: absolute;
top: 0px;
background-image: none;
filter: alpha(opacity :   50);
}
.#dialog {
visibility: hidden;
background-color: #f7fcfe;
z-index: 100;
width: 300px;
height: 50px;
position: absolute;
text-align: center;
font-size: 30px;
color: #FF0000;
font-weight: bold;
vertical-align: middle;
}
--></mce:style><style mce_bogus="1">.#mask {
visibility: hidden;
background-color: #cccccc;
left: 0px;
position: absolute;
top: 0px;
background-image: none;
filter: alpha(opacity :   50);
}
.#dialog {
visibility: hidden;
background-color: #f7fcfe;
z-index: 100;
width: 300px;
height: 50px;
position: absolute;
text-align: center;
font-size: 30px;
color: #FF0000;
font-weight: bold;
vertical-align: middle;
}</style>

<mce:script language="javaScript"><!--
function show()
{
var d_mask=document.getElementById('mask');
var d_dialog = document.getElementById('dialog');

d_mask.style.width = document.body.clientWidth ;
d_mask.style.height=document.body.clientHeight;
//网页正文全文
//d_mask.style.width = document.body.scrollWidth ;
//d_mask.style.height=document.body.scrollHeight;

d_dialog.style.top = document.body.clientHeight / 2 - 60;
d_dialog.style.left =document.body.clientWidth / 2 -100;
d_mask.style.visibility='visible';
d_dialog.style.visibility='visible';
}

function divBlock_event_mousedown()
{
var e, obj, temp;
obj=document.getElementById('dialog');
e=window.event?window.event:e;
obj.startX=e.clientX-obj.offsetLeft;
obj.startY=e.clientY-obj.offsetTop;
document.onmousemove=document_event_mousemove;
temp=document.attachEvent?document.attachEvent('onmouseup',document_event_mouseup):document.addEventListener('mouseup',document_event_mouseup,'');
}

function document_event_mousemove(e)
{
var e, obj;
obj=document.getElementById('dialog');
e=window.event?window.event:e;
with(obj.style){
position='absolute';
left=e.clientX-obj.startX+'px';
top=e.clientY-obj.startY+'px';
}
}
function document_event_mouseup(e)
{
var temp;
document.onmousemove='';
temp=document.detachEvent?document.detachEvent('onmouseup',document_event_mouseup):document.removeEventListener('mouseup',document_event_mouseup,'');
}

window.onresize = function()
{
var d_mask=document.getElementById('mask');
var d_dialog = document.getElementById('dialog');

d_mask.style.width = document.body.clientWidth ;
d_mask.style.height=document.body.clientHeight;
}
// --></mce:script>
</head>
<div id ="mask"></div>
<div id ="dialog" onmousedown="divBlock_event_mousedown()">处理中,请等待……</div>
<body>
<table border='0' width="100%" height="100%">
<tr>
<td>
测试
</td>
</tr>
<tr>
<td>
<input type="button" value="显示div" onclick="show()" />
</td>
</tr>
</table>
</body>
</html>


我直接用的方案一,方案二只是把代码拷过来测试了一下,可以用的。

(注:代码是网上收集的,不是我的原创,写在这里只是为了做个笔记,以免用到了还要四处去找,呵呵)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: