MiziaQ
MiziaQ

Reputation: 137

CheckBoxList Problem

I have a checkboxlist in C# that is databound from the database. What I would like to do is display an icon on the right side after the text for each of the checkboxlist items. Each icon needs to be different though. Thanks for your help!

Upvotes: 2

Views: 1111

Answers (3)

Orry
Orry

Reputation: 2648

You can apply a css class to each data bound checkbox and display an icon with CSS

Upvotes: 0

brichins
brichins

Reputation: 4055

We've used an approach that is fairly similar, but moves the code to the object model to simplify implementation on multiple pages.

Read-only property in the object code (C#):

public string chk_item_html { get { return item_name + string.Format("<img src='item{0}.png' />", item_id); } }

And then in the asp:CheckBoxList, you simply set DataTextField = "chk_item_html"

Upvotes: 1

Bala R
Bala R

Reputation: 108937

You could do something like this

<asp:CheckBoxList ID="CheckBoxList1" runat="server"
DataSourceID="dataSourceID"
DataTextField="dataTextField"
DataValueField="dataTextValue"
OnDataBound="CheckBoxList1_DataBound">
</asp:CheckBoxList>

protected void CheckBoxList1_DataBound(object sender, EventArgs e)
{
    var checkBox = sender as CheckBoxList;
    if(checkBox != null)
    {
        foreach (ListItem listItem in checkBox.Items)
        {
            listItem.Text = string.Format("{0}<img src='{1}' />", listItem.Text, GetImageFor(listItem.Text));
        }
    }

}

private string GetImageFor(string text)
{
    // return image url for check box based on text.

    if(text.Equals("Banana")) return "banana.gif";
    ...
    ...
}

Upvotes: 1

Related Questions