我们在提交一个表单的时候,可能由于网络或服务器的原因,处理很慢,而用户在处理结果出来之前反复点击按钮提交。这样很容易造成不必要的麻烦甚至是错误。说了这么多,其实就是要实现一个禁用某些控件的一种功能。好了,下面我就介绍自己简单实现的这个小功能。
<div class="codetitle" style="border-left-color: rgb(0, 153, 204); border-left-width: 1px; border-left-style: solid; padding: 0px 3px; margin: 3px auto 0px; width: 640px; clear: both; font-size: 14px; border-top-color: rgb(0, 153, 204); border-top-width: 1px; border-top-style: solid; border-right-color: rgb(0, 153, 204); border-right-width: 1px; border-right-style: solid; line-height: 25.2000007629395px; font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; background: rgb(242, 246, 251);">代码如下:</div><div class="codebody" id="code97423" style="border: 1px solid rgb(0, 153, 204); padding: 0px 3px 0px 5px; margin: 0px auto 3px; width: 638px; clear: both; font-size: 14px; line-height: 25.2000007629395px; font-family: Tahoma, Helvetica, Arial, 宋体, sans-serif; background: rgb(221, 237, 251);">
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 
} 
} </div>

在aspx页面中的调用如下:

复制代码代码如下:


protected void Page_Load(object sender, EventArgs e) 

{ 

if (!IsPostBack) 

{ 

ControlHelper.SetControlsEnabled(this.Page, ControlNameEnum.Panel, false); //Panel禁用 

} 

} 

asp.net 简单实现禁用或启用页面中的某一类型的控件的更多相关文章

  1. 在asp.net页面上按回车会触发Imagebutton控件的Click事件

    原文:在asp.net页面上按回车会触发Imagebutton控件的Click事件 问题: 用asp.net做的aspx页面,无论是否有文本框.下拉框.复选框……获得焦点,只要在当前页面上按一下回车就 ...

  2. ASP.NET中页面加载时文本框(texbox控件)内有文字获得焦点时文字消失

    代码如下: <asp:TextBox ID="TextBox1" runat="server" Height="26px" MaxLe ...

  3. ASP.NET MVC页面UI之联动下拉选择控件(省、市、县联动选择)

    地区选择操作在WEB应用中比较常见的操作,本文在.net mvc3下实现了省市县三级联动选择功能. 本文博客出处:http://www.kwstu.com/ArticleView/admin_2013 ...

  4. ASP.NET中共有哪几种类型的控件?其中,HTML控件、HTML服务器控件和WEB服务器控件之间有什么区别

    ASP.NET的控件包括WEB服务器控件.WEB用户控件.WEB自定义控件.HTML服务器控件和HTML控件.HTML控件.HTML服务器控件和WEB服务器控件之间的区别如下所示.q      HTM ...

  5. ASP.NET中的FileUpload文件上传控件的使用

    本篇文章教大家如何将客户端的图片或者文件上传到服务器: 无论是上传图片(.jpg .png .gif等等) 文档(word excel ppt 等等). 第一步:放入以下三个控件 Image控件,Fi ...

  6. ASP.NET中多个相同name的控件在后台正确取值

    有兽,   页面上可能有多个相同name的Html表单控件,   一般在后台使用Request.Form[“name”]取值,并用‘,’分隔.   但是当值中包含逗号时,   取值就会出现异常,   ...

  7. .net mvc页面UI之Jquery博客日历控件

    摘要:最近在做一个博客系统,其他需要用到博客日历控件,网上搜索了很多资料,其中大部分都是javascript的,经过总结使用jquery实现了博客日历效果.代码如下: 原文链接转载请注明:http:/ ...

  8. IOS中UITextView(多行文本框)控件的简单用法

    1.创建并初始化 UITextView文本视图相比与UITextField直观的区别就是UITextView可以输入多行文字并且可以滚动显示浏览全文.UITextField的用处多,UITextVie ...

  9. python简单爬虫 用lxml解析页面中的表格

    目标:爬取湖南大学2018年在各省的录取分数线,存储在txt文件中 部分表格如图: 部分html代码: <table cellspacing="0" cellpadding= ...

随机推荐

  1. Microsoft JScript 运行时错误: Sys.WebForms.PageRequestManagerParserErrorException无法分析从服务器收到的消息。之所以出现此错误,

    Microsoft JScript 运行时错误: Sys.WebForms.PageRequestManagerParserErrorException: 无法分析从服务器收到的消息.之所以出现此错误 ...

  2. oc语言学习之基础知识点介绍(一):OC介绍

      一.第一个OC程序 #import <Foundation/Foundation.h> //导入头文件 int main(int argc, const char * argv[]) ...

  3. C# lazy加载

    转自http://www.cnblogs.com/yunfeifei/p/3907726.html 在.NET4.0中,可以使用Lazy<T> 来实现对象的延迟初始化,从而优化系统的性能. ...

  4. glassfish 日志输出配置

    asadmin set-log-levels javax.enterprise.system.tools.deployment=WARNING

  5. 性能更好的js动画实现方式——requestAnimationFrame

    本文转载,原文地址:http://www.cnblogs.com/2050/p/3871517.html 用js来实现动画,我们一般是借助setTimeout或setInterval这两个函数,css ...

  6. 02_setter注入

    工程截图如下 [HelloWorld.java] package com.HigginCui; public class HelloWorld { private String words; publ ...

  7. JAVA时钟

    效果图如下: //简单动态时钟程序,以图形和数字两种方式来显示当前时间 import javax.swing.*; import java.awt.event.*; import java.awt.* ...

  8. Colored Linux Man pages

    Colored Linux Man pages 一.什么是Linux Man 参考: 二.如何高效率地使用Man 三.给Linux Man命令添加点颜色. 1.Unix / Linux: Displa ...

  9. c# DataTable 中 Select 和 Clone 用法结合

    C# DataTable是存放数据的一个离线数据库,将数据一下加载到内存. DataTable.Select ()方法: Select();//全部查出来    Select(过滤条件);//根据过滤 ...

  10. 程序员面试题精选100题(33)-在O(1)时间删除链表结点[数据结构]

    作者:何海涛 出处:http://zhedahht.blog.163.com/ 题目:给定链表的头指针和一个结点指针,在O(1)时间删除该结点.链表结点的定义如下: struct ListNode { ...