asp.net簡單實現禁用或啟用頁面中的某一類型的控制項

來源:互聯網
上載者:User
最近在一個winform項目中碰到的一個功能,勾選一個checkbox後表單中的其他控制項不可用。由此想到asp.net項目中有時候也要用到這種功能。比如,我們在提交一個表單的時候,可能由於網路或伺服器的原因,處理很慢,而使用者在處理結果出來之前反覆點擊按鈕提交。這樣很容易造成不必要的麻煩甚至是錯誤。說了這麼多,其實就是要實現一個禁用某些控制項的一種功能。好了,下面我就介紹自己簡單實現的這個小功能,貼代碼:Code
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頁面中的調用如下:Code
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ControlHelper.SetControlsEnabled(this.Page, ControlNameEnum.Panel, false); //Panel禁用
            }
        }

需要注意的是,我這裡的實現只是針對幾種常用控制項,您可以按照自己項目的需要任意擴充。
demo下載:demo

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.