Dropdownlist统一配置(从xml或枚举读取数据),应用文件依赖缓存.

来源:互联网 发布:淘宝hd版登录失败 编辑:程序博客网 时间:2024/06/05 11:00

1.从xml中读取配置信息 

/// <summary>
        /// 重新绑定下拉配
        /// </summary>
        /// <param name="drp">要绑定的下拉框</param>
        /// <param name="nodeName">xml节点名称</param>
        /// <param name="exceptLineName">不显示的项,支持多项,以","隔开</param>
        /// <param name="selectedVale">要选中的项</param>
        public static void BindDrpLine(DropDownList drp, string nodeName, string exceptLineName,string selectedValue)
        {
            drp.Items.Clear();
            drp.Items.Add(new ListItem("请选择...", "0"));
            string drpLineCahe = (string)HttpContext.Current.Cache[nodeName];
            if (drpLineCahe == null)
            {
                string filename = HttpContext.Current.Server.MapPath("~/DrpLine.xml");
                XmlDocument xmlDrpLine = new XmlDocument();
                xmlDrpLine.Load(filename);
                XmlNode node = xmlDrpLine.SelectSingleNode(nodeName);
                string strDrpLine = node.InnerText;
                HttpContext.Current.Cache.Insert(nodeName, strDrpLine, new System.Web.Caching.CacheDependency(filename));
                drpLineCahe = (string)HttpContext.Current.Cache[nodeName];
            }
            string[] arrDrpLine = drpLineCahe.Split(';');
            string[] drpLineVale;
            foreach (string arr in arrDrpLine)
            {
                drpLineVale = arr.Split(':');
                if (drpLineVale.Length == 2)
                {
                    if (!exceptLineName.Contains(drpLineVale[1].Trim()))
                    {
                        drp.Items.Add(new ListItem(drpLineVale[1].Trim(), drpLineVale[0].Trim()));
                    }
                }
            }
            if (selectedValue.Length > 0)
            {
                drp.SelectedValue = selectedValue;
            }
        }

2.从枚举类型读取配置信息.

    private void BindDropDown(DropDownList drp, string exceptProductName)
    {
        drp.Items.Clear();
        foreach (int i in Enum.GetValues(typeof(ProductLine)))
        {
            string strEnumName = Enum.GetName(typeof(ProductLine), i).ToString();
            if (!exceptProductName.Contains(strEnumName))
            {
                drp.Items.Add(new ListItem(strEnumName, i.ToString()));
            }
        }
    }
    public  enum ProductLine
    {
        安防 = 1, //是否显示边框 
        升腾 = 2, //是否显示标题 
        数字 = 3 //是否显示工具箱 
    }

原创粉丝点击