我找到窃听器了。显而易见:我没有将数据绑定到正确的checkboxlist!我应该将数据绑定到filterONTYPElist,但我正在将数据绑定到filterONDATASETlist...复制粘贴错误,抱歉...
我有一个核对框列表,它呈现如下:

下面是处理数据绑定的代码:
FilterOnTypeCheckboxList.DataSource = listCheckboxItems;
FilterOnDatasetCheckboxList.DataValueField = "Value";
FilterOnDatasetCheckboxList.DataTextField = "Text";
FilterOnTypeCheckboxList.DataBind();我的数据源是一个list<CheckBoxItem>。该类如下所示,您可以清楚地看到其中有一个公共属性值和一个公共属性文本:
[Serializable]
public class CheckboxItem
{
public string Text { get; set; }
public string Value { get; set; }
public CheckboxItem(string value, string text)
{
Value = value;
Text = text;
}
public override string ToString()
{
return "brompot";
}
}但是由于某些原因,每个复选框的文本和值都使用CheckBoxItem类的ToString()方法,而不是适当的属性" value“和" text”。
PS:我检查了checkboxitem对象的值和文本不是字符串"brompot"...
不能让toString()方法返回文本或值,因为我希望复选框值是value属性和复选框(标签)文本
发布于 2014-01-09 23:25:27
我运行了一个快速测试,这似乎如预期的那样工作。你能提供更多的细节吗?另外,验证我提供的代码是否与您正在做的类似?
<div>
<asp:Button ID="btnBind" runat="server" Text="Bind" OnClick="btnBind_Click" />
<asp:CheckBoxList ID="cbList" runat="server"></asp:CheckBoxList>
</div>
public partial class _Default : Page
{
protected void btnBind_Click(object sender, EventArgs e)
{
List<CheckboxItem> listCheckboxItems = new List<CheckboxItem>();
listCheckboxItems.Add(new CheckboxItem("Val-1", "Item-1"));
listCheckboxItems.Add(new CheckboxItem("Val-2", "Item-2"));
listCheckboxItems.Add(new CheckboxItem("Val-3", "Item-3"));
listCheckboxItems.Add(new CheckboxItem("Val-4", "Item-4"));
listCheckboxItems.Add(new CheckboxItem("Val-5", "Item-5"));
this.cbList.DataSource = listCheckboxItems;
this.cbList.DataValueField = "Value";
this.cbList.DataTextField = "Text";
this.cbList.DataBind();
}
}
[Serializable]
public class CheckboxItem
{
public string Text { get; set; }
public string Value { get; set; }
public CheckboxItem(string value, string text)
{
Value = value;
Text = text;
}
public override string ToString()
{
return "brompot";
}
}

发布于 2014-01-10 00:42:51
我相信你的错误是因为你的ToString()方法。
编辑成这样,看看这是否解决了你的问题:
[Serializable]
public class CheckboxItem
{
public string Text { get; set; }
public string Value { get; set; }
public CheckboxItem(string value, string text)
{
Value = value;
Text = text;
}
public override string ToString()
{
return Text;
}
}https://stackoverflow.com/questions/21023639
复制相似问题