最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

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

相關(guān)文章

  • AutoCAD .Net禁止圖元被刪除的方法

    AutoCAD .Net禁止圖元被刪除的方法

    這篇文章主要為大家詳細(xì)介紹了AutoCAD .Net禁止圖元被刪除的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Visual Studio 2010 前端開發(fā)工具/擴(kuò)展/插件推薦

    Visual Studio 2010 前端開發(fā)工具/擴(kuò)展/插件推薦

    這篇文章主要介紹了一組我喜愛的擴(kuò)展和工具能讓Visual Studio在web開發(fā)方面更簡單,我只是集中在我安裝和使用過的一些工具,如果你還有其它好用的的話,請?jiān)谶@里留言。
    2016-06-06
  • ASP.NET Core擴(kuò)展庫之Http通用擴(kuò)展庫的使用詳解

    ASP.NET Core擴(kuò)展庫之Http通用擴(kuò)展庫的使用詳解

    這篇文章主要介紹了ASP.NET Core擴(kuò)展庫之Http通用擴(kuò)展庫的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用.net技術(shù),感興趣的朋友可以了解下
    2021-04-04
  • ajaxControlToolkit中CascadingDropDown的用法說明

    ajaxControlToolkit中CascadingDropDown的用法說明

    今天頭叫寫一個(gè)類似三級(jí)聯(lián)動(dòng)的控件,最好實(shí)現(xiàn)無刷新,是石油軟件中的一個(gè)數(shù)據(jù),需要表現(xiàn)出類似 X1-22 這樣的格式,上下標(biāo)的數(shù)據(jù)是固定的 想了很多辦法來表現(xiàn)這個(gè)數(shù)字,最后決定用3個(gè)DropDownList控件
    2008-11-11
  • .Net頁面局部更新引發(fā)的思考

    .Net頁面局部更新引發(fā)的思考

    這篇文章主要是由.Net頁面局部更新引發(fā)的一系列思考,整理了實(shí)現(xiàn)局部更新的解決方案及改進(jìn)方案,感興趣的小伙伴們可以參考一下
    2016-06-06
  • asp.net中url字符串編碼亂碼的原因與解決方法

    asp.net中url字符串編碼亂碼的原因與解決方法

    這篇文章來給大家總結(jié)一下關(guān)于asp.net中url字符串編碼亂碼的原因與解決方法,有需要了解的朋友可以參考一下
    2013-08-08
  • 微信公眾號(hào)支付(MVC版本)

    微信公眾號(hào)支付(MVC版本)

    這篇文章主要為大家詳細(xì)介紹了微信公眾號(hào)支付,提供MVC版本,感興趣的小伙伴們可以參考一下
    2016-08-08
  • 詳解ASP.NET MVC3:Razor的@:和語法

    詳解ASP.NET MVC3:Razor的@:和語法

    這篇文章主要介紹了詳解ASP.NET MVC3:Razor的@:和語法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • .Net?core?Blazor+自定義日志提供器實(shí)現(xiàn)實(shí)時(shí)日志查看器的原理解析

    .Net?core?Blazor+自定義日志提供器實(shí)現(xiàn)實(shí)時(shí)日志查看器的原理解析

    我們經(jīng)常遠(yuǎn)程連接服務(wù)器去查看日志,比較麻煩,如果直接訪問項(xiàng)目的某個(gè)頁面就能實(shí)時(shí)查看日志就比較奈斯了,結(jié)合blazor實(shí)現(xiàn)了基本效果,這篇文章主要介紹了.Net?core?Blazor+自定義日志提供器實(shí)現(xiàn)實(shí)時(shí)日志查看器,需要的朋友可以參考下
    2022-10-10
  • ASP.NET?Core自定義中間件的方式詳解

    ASP.NET?Core自定義中間件的方式詳解

    這篇文章主要介紹了ASP.NET?Core自定義中間件的方式,雖然ASP.NET?Core為我們提供了一組豐富的內(nèi)置中間件,但有些時(shí)候我們可能會(huì)需要自定義一些中間件,將其穿插到管道中,以便滿足我們特定業(yè)務(wù)場景的需求,所以本文將介紹3種方式來滿足自定義中間件的需求
    2022-08-08

最新評(píng)論

海丰县| 乳山市| 大埔区| 惠安县| 会同县| 永顺县| 宜章县| 石屏县| 家居| 海兴县| 延吉市| 莱阳市| 京山县| 海伦市| 大竹县| 临安市| 永泰县| 信阳市| 绥棱县| 前郭尔| 河池市| 烟台市| 米泉市| 黔西县| 鄂托克旗| 永年县| 陕西省| 定西市| 惠水县| 玛沁县| 新泰市| 临桂县| 宽甸| 依安县| 内乡县| 收藏| 河间市| 德昌县| 阿鲁科尔沁旗| 郧西县| 永宁县|