欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 简单实现禁用或启用页面中的某一类型的控件

程序员文章站 2024-03-08 18:35:10
比如,我们在提交一个表单的时候,可能由于网络或服务器的原因,处理很慢,而用户在处理结果出来之前反复点击按钮提交。这样很容易造成不必要的麻烦甚至是错误。说了这么多,其实就是要...
比如,我们在提交一个表单的时候,可能由于网络或服务器的原因,处理很慢,而用户在处理结果出来之前反复点击按钮提交。这样很容易造成不必要的麻烦甚至是错误。说了这么多,其实就是要实现一个禁用某些控件的一种功能。好了,下面我就介绍自己简单实现的这个小功能,贴代码:
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.web;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
namespace dotnet.common.util
{
/// <summary>
/// 控件枚举,我们在禁用或启用时,就是根据这个枚举来匹配合适的项
/// </summary>
public enum controlnameenum
{
panel = 0, //容器 这个比较常用
textbox = 1,
button = 2, //这个也比较常用 比如 按钮提交后的禁用,返回结果后启用
checkbox = 3,
listcontrol = 4,
all = 100 //所有
}
public static class controlhelper
{
#region 同时禁用或者启用页面的某些控件
/// <summary>
/// 设置是否启用控件
/// </summary>
/// <param name="control"></param>
/// <param name="controlname"></param>
/// <param name="isenable"></param>
public static void setcontrolsenabled(control control, controlnameenum controlname, bool isenabled)
{
foreach (control item in control.controls)
{
/* 我们仅仅考虑几种常用的asp.net服务器控件和html控件 */
//panel
if (item is panel && (controlname == controlnameenum.panel || controlname == controlnameenum.all))
{
((panel)item).enabled = isenabled;
}
//textbox,htmltextbox
if (controlname == controlnameenum.textbox || controlname == controlnameenum.all)
{
if (item is textbox)
{
((textbox)(item)).enabled = isenabled;
}
else if (item is htmlinputtext)
{
((htmlinputtext)item).disabled = isenabled;
}
else if (item is htmltextarea)
{
((htmltextarea)(item)).disabled = isenabled;
}
}
//buttons
if (item is button && (controlname == controlnameenum.button || controlname == controlnameenum.all))
{
if (item is button)
{
((button)(item)).enabled = isenabled;
}
else if (item is htmlinputbutton)
{
((htmlinputbutton)(item)).disabled = !isenabled;
}
else if (item is imagebutton)
{
((imagebutton)(item)).enabled = isenabled;
}
else if (item is linkbutton)
{
((linkbutton)(item)).enabled = isenabled;
}
}
//checkbox
if (controlname == controlnameenum.checkbox || controlname == controlnameenum.all)
{
if (item is checkbox)
{
((checkbox)(item)).enabled = isenabled;
}
else if (item is htmlinputcheckbox)
{
((htmlinputcheckbox)(item)).disabled = !isenabled;
}
}
//list controls
if (controlname == controlnameenum.listcontrol || controlname == controlnameenum.all)
{
if (item is dropdownlist)
{
((dropdownlist)(item)).enabled = isenabled;
}
else if (item is radiobuttonlist)
{
((radiobuttonlist)(item)).enabled = isenabled;
}
else if (item is checkboxlist)
{
((checkboxlist)(item)).enabled = isenabled;
}
else if (item is listbox)
{
((listbox)(item)).enabled = isenabled;
}
else if (item is htmlselect)
{
((htmlselect)(item)).disabled = !isenabled;
}
}
//如果项目还有子控件,递归调用该函数
if (item.controls.count > 0)
{
setcontrolsenabled(item, controlname, isenabled);
}
}
}
#endregion
}
}

在aspx页面中的调用如下:
复制代码 代码如下:

protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
controlhelper.setcontrolsenabled(this.page, controlnameenum.panel, false); //panel禁用
}
}

需要注意的是,我这里的实现只是针对几种常用控件,您可以按照自己项目的需要任意扩展。
测试打包下载