首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CheckboxList忽略DataValueField和DataTextField

CheckboxList忽略DataValueField和DataTextField
EN

Stack Overflow用户
提问于 2014-01-09 22:58:05
回答 2查看 1.5K关注 0票数 2

我找到窃听器了。显而易见:我没有将数据绑定到正确的checkboxlist!我应该将数据绑定到filterONTYPElist,但我正在将数据绑定到filterONDATASETlist...复制粘贴错误,抱歉...

我有一个核对框列表,它呈现如下:

下面是处理数据绑定的代码:

代码语言:javascript
复制
FilterOnTypeCheckboxList.DataSource = listCheckboxItems;
FilterOnDatasetCheckboxList.DataValueField = "Value";
FilterOnDatasetCheckboxList.DataTextField = "Text";
FilterOnTypeCheckboxList.DataBind();

我的数据源是一个list<CheckBoxItem>。该类如下所示,您可以清楚地看到其中有一个公共属性值和一个公共属性文本:

代码语言:javascript
复制
[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属性和复选框(标签)文本

EN

回答 2

Stack Overflow用户

发布于 2014-01-09 23:25:27

我运行了一个快速测试,这似乎如预期的那样工作。你能提供更多的细节吗?另外,验证我提供的代码是否与您正在做的类似?

代码语言:javascript
复制
<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";
    }
}

票数 3
EN

Stack Overflow用户

发布于 2014-01-10 00:42:51

我相信你的错误是因为你的ToString()方法。

编辑成这样,看看这是否解决了你的问题:

代码语言:javascript
复制
[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;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21023639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档