zoukankan      html  css  js  c++  java
  • 扩展DropDownList

    使用DropDownList,我们习惯在所有待选数据前面加一个空白项,或者"请选择"的提示,微软却没有,这可能就是中西文化的差异.

    现在扩展为DropDownListPro

    经过代码调试,和看源代码,得知默认选中的行为是在SelectedIndex的Get方法中设置的.所以重载该方法,即可达到目的.

     public class DropDownListPro: DropDownList
        {
            public bool AddBlankItem
            {
                get
                {
                    object obj2 = base.ViewState["AddBlankItem"];
                    if (obj2 != null)
                    {
                        return (bool)obj2;
                    }
                    return false;
                }
                set
                {
                    if (!object.Equals(value, base.ViewState["AddBlankItem"]))
                    {
                        base.ViewState["AddBlankItem"] = value;
                    }
                }
            }
            private bool m_HasAdded=false;
            public override int SelectedIndex
            {
                get
                {
                    if (!m_HasAdded && AddBlankItem)
                    {
                        this.Items.Insert(0, new ListItem(" ", "-1"));
                        m_HasAdded = true;
                    }               
                    return base.SelectedIndex;
                }
                set
                {
                    base.SelectedIndex = value;
                }
            }

    }

    在使用的时候只需要设置 DropDownList1.AddBlankItem=true.

  • 相关阅读:
    Python中的logging模块
    Windows 域(domain)
    linux下查看某软件是否已安装, ubuntu安装deb包
    linux之iptables
    linux之flock函数锁文件
    《上班赚钱,下班理财》
    Get the image file(s) some informations,Including the Make,Model,Date/Time,etc
    java中的Integer的toBinaryString()方法
    面试题中遇到的SQL题目
    get和post方法的区别
  • 原文地址:https://www.cnblogs.com/DataFlow/p/1446896.html
Copyright © 2011-2022 走看看