码迷,mamicode.com
首页 > 其他好文 > 详细

绑定枚举到dropdownlist

时间:2014-07-16 15:16:44      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:os   for   re   c   new   ar   

 

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);
        }
    }

绑定枚举到dropdownlist,布布扣,bubuko.com

绑定枚举到dropdownlist

标签:os   for   re   c   new   ar   

原文地址:http://www.cnblogs.com/itjeff/p/3848293.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!