【问题标题】:CheckboxList ignores DataValueField and DataTextFieldCheckboxList 忽略 DataValueField 和 DataTextField
【发布时间】:2014-01-28 04:56:07
【问题描述】:

我发现了错误。很明显:我没有对正确的复选框列表进行数据绑定!我应该对filterONTYPElist进行数据绑定,但我正在对filterONDATASETlist进行数据绑定...复制粘贴错误,抱歉...

我有一个复选框列表,呈现如下:

这是处理数据绑定的代码:

FilterOnTypeCheckboxList.DataSource = listCheckboxItems;
FilterOnDatasetCheckboxList.DataValueField = "Value";
FilterOnDatasetCheckboxList.DataTextField = "Text";
FilterOnTypeCheckboxList.DataBind();

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

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

但由于某种原因,每个复选框的文本 AND 值使用 CheckBoxItem 类的 ToString() 方法,而不是适当的属性“Value”和“Text”。

PS:我检查了 checkboxitems 对象的值和文本不是字符串“brompot”...

让 toString() 方法返回文本或值不是一个选项,因为我希望复选框值是值属性和复选框(标签)文本

【问题讨论】:

    标签: c# checkboxlist


    【解决方案1】:

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

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

    【讨论】:

    • 这正是我想要做的,只是我没有看到值或文本,而是 toString() 方法返回的任何内容。如果我没有指定 toString() 方法,c# 将采用默认的 ToString() 方法,该方法返回“CheckBoxItem”的类名。我可以忽略什么?
    • 这只是一个猜测,但请尝试删除该类的 [Serializable] 标签。
    【解决方案2】:

    我相信你的错误是因为你的 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;
      }
    }
    

    【讨论】:

    • 感谢您的建议,但您提出的解决方案存在值和文本字段都将变为“文本”的问题。
    猜你喜欢
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多