首页 > 其他 > 详细

绑定枚举到dropdownlist

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

 

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!