Checkboxlist with Image in ASP.NET

Put the checkboxlist control on to aspx page as follows:
  1. <asp:CheckBoxList ID="checkboxlist1" AutoPostBack="True"   
  2. RepeatDirection="Horizontal" TextAlign="Right" runat="server"   
  3. RepeatColumns = 5 ondatabound="checkboxlist1_DataBound">  
  4. </asp:CheckBoxList>  
Bind the checkboklist control on codebehind as follows: 
  1. private void FillIcons()  
  2. {   
  3.  //Fatch the record from database and contain into datatable   
  4.     if (datatable.Rows.Count > 0)  
  5.     {   
  6.         checkboxlist1.DataSource = datatable;  
  7.         checkboxlist1.DataTextField = "ImageName";  
  8.         checkboxlist1.DataValueField = "Name";  
  9.         checkboxlist1.DataBind();  
  10.     }  
  11. }  
Add these lines for displaying the image. 
  1. protected void checkboxlist1_DataBound(object sender, EventArgs e)  
  2. {  
  3.     foreach (ListItem item in checkboxlist1.Items)  
  4.     {           
  5.         item.Text = string.Format("<img src= \"{0}\" title='" + item.Value + "'/>",   
  6.         string.Format("images/{0}", item.Text), null);  
  7.     }  
  8. }  
Ebook Download
View all
Learn
View all