ITPub博客

首页 > IT职业 > IT职场 > 高亮CheckBoxList选中的项目

高亮CheckBoxList选中的项目

IT职场 作者:ybbyic 时间:2021-09-09 10:11:55 0 删除 编辑


 

准备数据:

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专案中,然后读出所有图片文件:

5acf07260001405b00110016.jpgView 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控件:

5acf07260001405b00110016.jpgView 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"事件。

5acf07260001405b00110016.jpgView 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"事件。

5acf07260001405b00110016.jpgView 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;");
            }
        }
    }

 

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1769/viewspace-2817989/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    88
  • 访问量
    1009