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

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

 

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

ExpandedBlockStart.gif View Code
private Dictionary< stringstring> Operation()
    {
        Dictionary< stringstring> o =  new Dictionary< stringstring>();
        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:

ExpandedBlockStart.gif View 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"事件:

ExpandedBlockStart.gif View 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);
        }
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值