• 高亮CheckBoxList选中的项目


    先看看效果:

    准备数据:

    http://www.cnblogs.com/insus/articles/1439030.html

    .aspx:

    <asp:CheckBoxList ID="CheckBoxListColour" runat="server" RepeatColumns="10" RepeatDirection="Horizontal"
                OnDataBound
    ="CheckBoxListColour_DataBound" OnSelectedIndexChanged="CheckBoxListColour_SelectedIndexChanged"
                AutoPostBack
    ="true">
            </asp:CheckBoxList>

    从上面的数据,下载并放入asp.net专案中,然后读出所有图片文件:

    View Code
     private List<string> ImageNames
        {
            get
            {
                List<string> o = new List<string>();

                DirectoryInfo di = new DirectoryInfo(Server.MapPath ("~/Colours"));
                FileInfo[] fiArray = di.GetFiles();
                for (int i = 0; i < fiArray.Length; i++)
                {
                    o.Add(fiArray[i].Name);
                }
                return o;
            }
        }

    绑定数据至CheckBoxList控件:

    View Code
     protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Data_Binding();
            }
        }

        private void Data_Binding()
        {
            this.CheckBoxListColour.DataSource = ImageNames.Select(c => new { value = c }).ToList();
            this.CheckBoxListColour.DataTextField = "value";
            this.CheckBoxListColour.DataBind();
        }

     CheckBoxList控件的OnDataBound="CheckBoxListColour_DataBound"事件。

    View Code
     protected void CheckBoxListColour_DataBound(object sender, EventArgs e)
        {
            var cbl = sender as CheckBoxList;
            foreach (ListItem li in cbl.Items)
            {
                li.Text = string.Format("<img src='Colours/{0}' />", li.Value);
            }
        }

    CheckBoxList控件的OnSelectedIndexChanged="CheckBoxListColour_SelectedIndexChanged"事件。

    View Code
    protected void CheckBoxListColour_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cbl = sender as CheckBoxList;
            foreach (ListItem li in cbl.Items)
            {
                if (li.Selected)
                {
                    li.Attributes.Add("style""background-color: red;");
                }
            }
        }
  • 相关阅读:
    Spring5源码分析(011)——IoC篇之解析bean标签总览
    Spring5源码分析(010)——IoC篇之加载BeanDefinition:解析和注册BeanDefinition
    Spring5源码分析(009)——IoC篇之加载BeanDefinition:获取 Document 实例
    Spring Boot2(015):JSON
    Spring Boot2(014):国际化 Internationalization
    Spring Boot2(013):日志
    Spring Boot2(012):Profiles
    Spring Boot2(011):外部化配置 Externalized Configuration
    Spring5源码分析(008)——IoC篇之加载BeanDefinition:获取XML的验证模式
    ubuntu 16.04下ssh访问提示错误
  • 原文地址:https://www.cnblogs.com/insus/p/2428761.html
Copyright © 2020-2023  润新知