欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net 簡(jiǎn)單實(shí)現(xiàn)禁用或啟用頁(yè)面中的某一類(lèi)型的控件

 更新時(shí)間:2009年11月22日 00:24:33   作者:  
最近在一個(gè)winform項(xiàng)目中碰到的一個(gè)功能,勾選一個(gè)checkbox后窗體中的其他控件不可用。由此想到asp.net項(xiàng)目中有時(shí)候也要用到這種功能。
比如,我們?cè)谔峤灰粋€(gè)表單的時(shí)候,可能由于網(wǎng)絡(luò)或服務(wù)器的原因,處理很慢,而用戶在處理結(jié)果出來(lái)之前反復(fù)點(diǎn)擊按鈕提交。這樣很容易造成不必要的麻煩甚至是錯(cuò)誤。說(shuō)了這么多,其實(shí)就是要實(shí)現(xiàn)一個(gè)禁用某些控件的一種功能。好了,下面我就介紹自己簡(jiǎn)單實(shí)現(xiàn)的這個(gè)小功能,貼代碼:
復(fù)制代碼 代碼如下:

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>
/// 控件枚舉,我們?cè)诮没騿⒂脮r(shí),就是根據(jù)這個(gè)枚舉來(lái)匹配合適的項(xiàng)
/// </summary>
public enum ControlNameEnum
{
Panel = 0, //容器 這個(gè)比較常用
TextBox = 1,
Button = 2, //這個(gè)也比較常用 比如 按鈕提交后的禁用,返回結(jié)果后啟用
CheckBox = 3,
ListControl = 4,
All = 100 //所有
}
public static class ControlHelper
{
#region 同時(shí)禁用或者啟用頁(yè)面的某些控件
/// <summary>
/// 設(shè)置是否啟用控件
/// </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服務(wù)器控件和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;
}
}
//如果項(xiàng)目還有子控件,遞歸調(diào)用該函數(shù)
if (item.Controls.Count > 0)
{
SetControlsEnabled(item, controlName, isEnabled);
}
}
}
#endregion
}
}

在aspx頁(yè)面中的調(diào)用如下:
復(fù)制代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ControlHelper.SetControlsEnabled(this.Page, ControlNameEnum.Panel, false); //Panel禁用
}
}

需要注意的是,我這里的實(shí)現(xiàn)只是針對(duì)幾種常用控件,您可以按照自己項(xiàng)目的需要任意擴(kuò)展。
測(cè)試打包下載

相關(guān)文章

最新評(píng)論