zoukankan      html  css  js  c++  java
  • 禁用或启用DropDownList的Items

    此篇算是对http://www.cnblogs.com/insus/archive/2012/04/17/2454620.html重构升级。

    因为网友需要不但能禁用还能可以启用DropDownList的Items。为了不想用户写太多代码。Insus.NET写了一个类别,并让它继承了System.Web.UI.WebControls命名空间下的DropDownList. 可从下图看到InsusDropDownList实例化并传入DropDownList控件,然后实例化之后的对象,就是可以使用highlight的四个方法DisableImsByText(), DisabletemsByVue() ,EnableItemsBText(), EnableItemsByValue()。

    InsusDropDownList类别:

    InsusDropDownList
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI.WebControls;

    /// <summary>
    /// Summary description for InsusDropDownList
    /// </summary>
    namespace Insus.NET
    {
        public class InsusDropDownList : DropDownList
        {
            DropDownList _DropDownList;

            public InsusDropDownList(DropDownList dropDownList)
            {
                this._DropDownList = dropDownList;
            }

            public void DisableItemsByText(string text)
            {          
                DisableItems(GetIndexByText(text));
            }

            public void EnableItemsByText(string text)
            {            
                EnableItems(GetIndexByText(text));
            }

            public void DisableItemsByValue(string value)
            {           
                DisableItems(GetIndexByValue(value));
            }

            public void EnableItemsByValue(string value)
            {            
                EnableItems(GetIndexByValue(value));
            }

            private int GetIndexByText(string text)
            {
                return this._DropDownList.Items.IndexOf(this._DropDownList.Items.FindByText(text));
            }

            private int GetIndexByValue(string value)
            {
                return this._DropDownList.Items.IndexOf(this._DropDownList.Items.FindByValue(value));
            }

            private void DisableItems(int index)
            {
                if (index > -1)
                    this._DropDownList.Items[index].Attributes.Add("disabled""disabled");
            }

            private void EnableItems(int index)
            {
                if (index > -1)
                    this._DropDownList.Items[index].Attributes.Remove("disabled");
            }
        }
    }

    演示,启用Items: 

     if (Request.QueryString["site"] != null)
            {
                InsusDropDownList obj = new InsusDropDownList(this.DropDownList1);
                obj.EnableItemsByText(Request.QueryString["site"]);
            }   
  • 相关阅读:
    iOS.UI_正则表达式(特殊字符)
    iOS.UI_正则(电话号码)
    iOS远程推送h获取Token
    CoreData教学完整版(封装我们自己的CoreData工具)_Dylan
    Swift基础加强_跟我打500行
    xmpp好友请求5
    XMPP教学小结1
    xmpp好友状态4
    XMPP好友列表3
    XMPP收发消息2
  • 原文地址:https://www.cnblogs.com/insus/p/2456828.html
Copyright © 2011-2022 走看看