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

 更新时间:2009年11月22日 00:24:33   作者:  
最近在一个winform项目中碰到的一个功能,勾选一个checkbox后窗体中的其他控件不可用。由此想到asp.net项目中有时候也要用到这种功能。
比如,我们在提交一个表单的时候,可能由于网络或服务器的原因,处理很慢,而用户在处理结果出来之前反复点击按钮提交。这样很容易造成不必要的麻烦甚至是错误。说了这么多,其实就是要实现一个禁用某些控件的一种功能。好了,下面我就介绍自己简单实现的这个小功能,贴代码:
复制代码 代码如下:

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禁用
}
}

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

相关文章

  • AutoCAD .Net禁止图元被删除的方法

    AutoCAD .Net禁止图元被删除的方法

    这篇文章主要为大家详细介绍了AutoCAD .Net禁止图元被删除的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Visual Studio 2010 前端开发工具/扩展/插件推荐

    Visual Studio 2010 前端开发工具/扩展/插件推荐

    这篇文章主要介绍了一组我喜爱的扩展和工具能让Visual Studio在web开发方面更简单,我只是集中在我安装和使用过的一些工具,如果你还有其它好用的的话,请在这里留言。
    2016-06-06
  • ASP.NET Core扩展库之Http通用扩展库的使用详解

    ASP.NET Core扩展库之Http通用扩展库的使用详解

    这篇文章主要介绍了ASP.NET Core扩展库之Http通用扩展库的使用详解,帮助大家更好的理解和学习使用.net技术,感兴趣的朋友可以了解下
    2021-04-04
  • ajaxControlToolkit中CascadingDropDown的用法说明

    ajaxControlToolkit中CascadingDropDown的用法说明

    今天头叫写一个类似三级联动的控件,最好实现无刷新,是石油软件中的一个数据,需要表现出类似 X1-22 这样的格式,上下标的数据是固定的 想了很多办法来表现这个数字,最后决定用3个DropDownList控件
    2008-11-11
  • .Net页面局部更新引发的思考

    .Net页面局部更新引发的思考

    这篇文章主要是由.Net页面局部更新引发的一系列思考,整理了实现局部更新的解决方案及改进方案,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • asp.net中url字符串编码乱码的原因与解决方法

    asp.net中url字符串编码乱码的原因与解决方法

    这篇文章来给大家总结一下关于asp.net中url字符串编码乱码的原因与解决方法,有需要了解的朋友可以参考一下
    2013-08-08
  • 微信公众号支付(MVC版本)

    微信公众号支付(MVC版本)

    这篇文章主要为大家详细介绍了微信公众号支付,提供MVC版本,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • 详解ASP.NET MVC3:Razor的@:和语法

    详解ASP.NET MVC3:Razor的@:和语法

    这篇文章主要介绍了详解ASP.NET MVC3:Razor的@:和语法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-01-01
  • .Net core Blazor+自定义日志提供器实现实时日志查看器的原理解析

    .Net core Blazor+自定义日志提供器实现实时日志查看器的原理解析

    我们经常远程连接服务器去查看日志,比较麻烦,如果直接访问项目的某个页面就能实时查看日志就比较奈斯了,结合blazor实现了基本效果,这篇文章主要介绍了.Net core Blazor+自定义日志提供器实现实时日志查看器,需要的朋友可以参考下
    2022-10-10
  • ASP.NET Core自定义中间件的方式详解

    ASP.NET Core自定义中间件的方式详解

    这篇文章主要介绍了ASP.NET Core自定义中间件的方式,虽然ASP.NET Core为我们提供了一组丰富的内置中间件,但有些时候我们可能会需要自定义一些中间件,将其穿插到管道中,以便满足我们特定业务场景的需求,所以本文将介绍3种方式来满足自定义中间件的需求
    2022-08-08

最新评论