ITPub博客

首页 > IT职业 > IT图书 > 为CheckBoxList每个项目添加一张图片

为CheckBoxList每个项目添加一张图片

IT图书 作者:hunter_tols 时间:2021-09-09 10:11:55 0 删除 编辑

参考下图,可看到效果,为CheckBoxList每个项目添加一张图片。


 

准备五张图片,如上图,和CheckBoxList项目数据:

5acf07270001405b00110016.jpgView Code private Dictionary<string, string> Operation()
    {
        Dictionary<string, string> o = new Dictionary<string, string>();
        o.Add("i","Insert");
        o.Add("e","Edit");
        o.Add("c","Cancel");
        o.Add("u","Update");
        o.Add("d","Delete");
        return o;
    }

 

然后在.aspx,并使用OnDataBound事件:

 <asp:CheckBoxList ID="CheckBoxListOperation" runat="server" RepeatColumns="1" RepeatDirection="Horizontal" OnDataBound="CheckBoxListOperation_DataBound">
            </asp:CheckBoxList>

 

.aspx.cs:

5acf07270001405b00110016.jpgView Code  protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Data_Binding();
        }
    }

    private void Data_Binding()
    {
        this.CheckBoxListOperation.DataSource = Operation();
        this.CheckBoxListOperation.DataTextField = "value";
        this.CheckBoxListOperation.DataValueField = "key";
        this.CheckBoxListOperation.DataBind();
    }

 

OnDataBound="CheckBoxListOperation_DataBound"事件:

5acf07270001405b00110016.jpgView Code protected void CheckBoxListOperation_DataBound(object sender, EventArgs e)
    {
        var cbl = sender as CheckBoxList;
        foreach (ListItem li in cbl.Items)
        {
            li.Text = string.Format("<img src='Images/{0}.gif' /> {1}", li.Value, li.Text);
        }
    }

 

 

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

下一篇: 被遗忘的初始化
请登录后发表评论 登录
全部评论

注册时间:2019-08-08

  • 博文量
    87
  • 访问量
    615