Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 复选框问题_C#_Asp.net_Checkboxlist - Fatal编程技术网

C# 复选框问题

C# 复选框问题,c#,asp.net,checkboxlist,C#,Asp.net,Checkboxlist,我有一个C语言的复选框列表,它是从数据库中绑定的。我想做的是在每个复选框列表项的文本之后的右侧显示一个图标。但每个图标都需要不同。谢谢你的帮助 你可以这样做 <asp:CheckBoxList ID="CheckBoxList1" runat="server" DataSourceID="dataSourceID" DataTextField="dataTextField" DataValueField="dataTextValue" OnDataBound="CheckBoxList1_

我有一个C语言的复选框列表,它是从数据库中绑定的。我想做的是在每个复选框列表项的文本之后的右侧显示一个图标。但每个图标都需要不同。谢谢你的帮助

你可以这样做

<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";
    ...
    ...
}

受保护的void CheckBoxList1_数据绑定(对象发送方,事件参数e)
{
var checkBox=发送方作为复选框列表;
如果(复选框!=null)
{
foreach(checkBox.Items中的ListItem ListItem)
{
listItem.Text=string.Format(“{0}”,listItem.Text,GetImageFor(listItem.Text));
}
}
}
私有字符串GetImageFor(字符串文本)
{
//基于文本返回复选框的图像url。
如果(text.Equals(“Banana”))返回“Banana.gif”;
...
...
}

我们使用了一种非常类似的方法,但将代码移动到对象模型以简化多页上的实现

对象代码(C#)中的只读属性:

公共字符串chk_item_html{get{return item_name+string.Format(“,item_id);}

然后在asp:CheckBoxList中,只需设置DataTextField=“chk_item_html”

就可以对每个数据绑定复选框应用一个css类,并显示一个带有css的图标。

什么帮助?我能从你的问题中读到的只是你希望它如何显示。
public string chk_item_html { get { return item_name + string.Format("<img src='item{0}.png' />", item_id); } }