asp.net 簡單實現(xiàn)禁用或啟用頁面中的某一類型的控件
更新時間:2009年11月22日 00:24:33 作者:
最近在一個winform項目中碰到的一個功能,勾選一個checkbox后窗體中的其他控件不可用。由此想到asp.net項目中有時候也要用到這種功能。
比如,我們在提交一個表單的時候,可能由于網(wǎng)絡或服務器的原因,處理很慢,而用戶在處理結(jié)果出來之前反復點擊按鈕提交。這樣很容易造成不必要的麻煩甚至是錯誤。說了這么多,其實就是要實現(xiàn)一個禁用某些控件的一種功能。好了,下面我就介紹自己簡單實現(xiàn)的這個小功能,貼代碼:
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>
/// 控件枚舉,我們在禁用或啟用時,就是根據(jù)這個枚舉來匹配合適的項
/// </summary>
public enum ControlNameEnum
{
Panel = 0, //容器 這個比較常用
TextBox = 1,
Button = 2, //這個也比較常用 比如 按鈕提交后的禁用,返回結(jié)果后啟用
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;
}
}
//如果項目還有子控件,遞歸調(diào)用該函數(shù)
if (item.Controls.Count > 0)
{
SetControlsEnabled(item, controlName, isEnabled);
}
}
}
#endregion
}
}
在aspx頁面中的調(diào)用如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ControlHelper.SetControlsEnabled(this.Page, ControlNameEnum.Panel, false); //Panel禁用
}
}
需要注意的是,我這里的實現(xiàn)只是針對幾種常用控件,您可以按照自己項目的需要任意擴展。
測試打包下載
復制代碼 代碼如下:
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>
/// 控件枚舉,我們在禁用或啟用時,就是根據(jù)這個枚舉來匹配合適的項
/// </summary>
public enum ControlNameEnum
{
Panel = 0, //容器 這個比較常用
TextBox = 1,
Button = 2, //這個也比較常用 比如 按鈕提交后的禁用,返回結(jié)果后啟用
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;
}
}
//如果項目還有子控件,遞歸調(diào)用該函數(shù)
if (item.Controls.Count > 0)
{
SetControlsEnabled(item, controlName, isEnabled);
}
}
}
#endregion
}
}
在aspx頁面中的調(diào)用如下:
復制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ControlHelper.SetControlsEnabled(this.Page, ControlNameEnum.Panel, false); //Panel禁用
}
}
需要注意的是,我這里的實現(xiàn)只是針對幾種常用控件,您可以按照自己項目的需要任意擴展。
測試打包下載
您可能感興趣的文章:
- ASP.NET:把ashx寫到類庫里并在頁面上調(diào)用的具體方法
- 遞歸輸出ASP.NET頁面所有控件的類型和ID的代碼
- asp.net 數(shù)據(jù)訪問層基類
- Asp.net 字符串操作基類(安全,替換,分解等)
- Asp.net 彈出對話框基類(輸出alet警告框)
- Asp.net 時間操作基類(支持短日期,長日期,時間差)
- Asp.Net 通用數(shù)據(jù)操作類 (附通用數(shù)據(jù)基類)
- Asp.Net+XML操作基類(修改,刪除,新增,創(chuàng)建)
- Asp.Net 文件操作基類(讀取,刪除,批量拷貝,刪除,寫入,獲取文件夾大小,文件屬性,遍歷目錄)
- asp.net實現(xiàn)非常實用的自定義頁面基類(附源碼)
相關文章
在ASP.NET Core中用HttpClient發(fā)送POST, PUT和DELETE請求
這篇文章主要介紹了在ASP.NET Core中用HttpClient發(fā)送POST, PUT和DELETE請求的方法,幫助大家更好的理解和學習使用ASP.NET Core,感興趣的朋友可以了解下2021-03-03ASP.NET MVC如何使用Unity實現(xiàn)Ioc詳解
這篇文章主要給大家介紹了關于ASP.NET MVC如何使用Unity實現(xiàn)Ioc的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-07-07在ASP.Net中實現(xiàn)flv視頻轉(zhuǎn)換的代碼
在ASP.Net中實現(xiàn)flv視頻轉(zhuǎn)換的代碼...2007-09-09asp.net(C#)防sql注入組件的實現(xiàn)代碼
經(jīng)常要寫一些.net的程序,對于數(shù)據(jù)庫的防注入要求要比較高。這時我從網(wǎng)上搜了一些代碼。查看了一下主要是通過HTTPModel來進行對客戶端轉(zhuǎn)過來的數(shù)據(jù)進行處理。2009-12-12ASP.NET調(diào)用WebService服務的方法詳解
這篇文章主要介紹了ASP.NET調(diào)用WebService服務的方法,較為詳細的分析了WebService服務的功能,創(chuàng)建步驟與使用方法,需要的朋友可以參考下2016-05-05ASP.NET中各種連接數(shù)據(jù)庫的配置的方法及json數(shù)據(jù)轉(zhuǎn)換
本篇文章主要介紹了ASP.NET中各種連接數(shù)據(jù)庫的配置的方法,詳細的介紹了MSSQL、Access、Oracle、SQLite、MySQL數(shù)據(jù)庫配置,具有一定的參考價值,有興趣的可以了解一下。2017-01-01