继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

喵喵一只汪
关注TA
已关注
手记 315
粉丝 86
获赞 467

参考下图,可看到效果,为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);
        }
    }

 

 

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP