How do I create a checkbox list with images in ASP.net?
I ha开发者_如何学Gove a requirement where I need to show images in a check box control.
Is there any way we can implement a check box list that contains images?
Instead of applying a text value to a checkbox control just assign the text value to an HTML img
tag.
<asp:CheckBox ID="yourCheckBox" runat="server" Text="<img src='yourimage.gif' alt='' title='' />" />
If this is in sometype of bound control template you could implement the OnDataBinding event for the checkbox and then modify the image based on your datasource contents as well.
If your using a checkboxlist control then you could even do it all though code:
// in your aspx
<asp:CheckBoxList ID="yourList" runat="server">
// in your .cs when you want to load your value assuming you have a list of images
foreach (yourCheckBoxDataObject x in youCheckBoxData)
{
yourList.Items.Add(new ListItem(
string.Format("<img src='{0}' alt='' />", x.YourImageUrl),
x.YourValue));
}
If they can be standard HTML tags, you could always embed them in the checkbox's Text value. That should render them.
<asp:CheckBoxList ID="x" runat="server">
<asp:ListItem Text="Image: <img src='Images/PlusSign.gif' /> works"></asp:ListItem>
<asp:ListItem Text="Image: <img src='Images/XSign.gif' /> works"></asp:ListItem>
</asp:CheckBoxList>
Perhaps you could use tag mapping to achieve the customization you seek?
http://leedumond.com/blog/fixing-asp-net-server-control-rendering-issues-with-tag-mapping/
Your other option other than using the HTML tags would be to extend the checkboxlist control and create your own custom control which implements a checkboxlist with embedded images.
精彩评论