首页 > 代码库 > 绑定枚举到dropdownlist

绑定枚举到dropdownlist

 

pageTools.BindEnumToDropdownList(typeof(enumDealerArea), ddlBmwArea, new ListItem("--请选择--", "0"), "");

 

 

/// <summary>
    /// 绑定枚举到dropdownlist
    /// </summary>
    /// <param name="enumType">枚举类型</param>
    /// <param name="ddl">dropdownlist</param>
    /// <param name="li">初始化第一个选项,可为null</param>
    /// <param name="selectValue">选中项的值</param>
    public void BindEnumToDropdownList(Type enumType, DropDownList ddl, ListItem li, string selectValue)
    {
        ddl.Items.Clear();
        if (li != null)
        {
            ddl.Items.Add(li);
        }

        foreach (int i in Enum.GetValues(enumType))
        {
            ListItem li2 = new ListItem(Enum.GetName(enumType, i), (i + 1).ToString());
            if (li2.Value.Equals(selectValue))
            {
                li2.Selected = true;
            }
            ddl.Items.Add(li2);
        }
    }


    /// <summary>
    /// 绑定枚举到dropdownlist
    /// </summary>
    /// <param name="enumType">枚举类型</param>
    /// <param name="ddl">dropdownlist</param>
    /// <param name="li">初始化第一个选项,可为null</param>
    /// <param name="selectValue">选中项的值</param>
    /// <param name="notValues">枚举中要排除的值</param>
    public void BindEnumToDropdownList2(Type enumType, DropDownList ddl, ListItem li, string selectValue, List<int> notValues)
    {
        ddl.Items.Clear();
        if (li != null)
        {
            ddl.Items.Add(li);
        }

        foreach (int i in Enum.GetValues(enumType))
        {
            if (notValues.Find(delegate(int o) { return o == i; }) == 0)
            {
                ListItem li2 = new ListItem(Enum.GetName(enumType, i), (i).ToString());
                if (li2.Value.Equals(selectValue))
                {
                    li2.Selected = true;
                }
                ddl.Items.Add(li2);
            }
        }
    }

    public void BindEnumToDropdownList2(Type enumType, DropDownList ddl, ListItem li, string selectValue)
    {
        ddl.Items.Clear();
        if (li != null)
        {
            ddl.Items.Add(li);
        }

        foreach (int i in Enum.GetValues(enumType))
        {
            ListItem li2 = new ListItem(Enum.GetName(enumType, i), i.ToString());
            if (li2.Value.Equals(selectValue))
            {
                li2.Selected = true;
            }
            ddl.Items.Add(li2);
        }
    }
    /// <summary>
    /// 绑定枚举到dropdownlist
    /// Jason_tang
    /// </summary>
    /// <param name="enumType"></param>
    /// <param name="ddl"></param>
    /// <param name="selectValue"></param>
    public void BindEnumToDropdownList3(Type enumType, DropDownList ddl, string selectValue)
    {
        ddl.Items.Clear();
        //if (li != null)
        //{
        //    ddl.Items.Add(li);
        //}

        foreach (int i in Enum.GetValues(enumType))
        {
            ListItem li2 = new ListItem(Enum.GetName(enumType, i), i.ToString());
            if (li2.Value.Equals(selectValue))
            {
                li2.Selected = true;
            }
            ddl.Items.Add(li2);
        }
    }