为什么这个 DropDownList 数据绑定到 List?不工作?

发布于 2024-09-28 16:45:13 字数 1490 浏览 2 评论 0原文

我正在尝试将 List 绑定到用户控件中的 DropDownList 。我认为我正在做正确的事情,但似乎在我的代码执行后绑定被清除。这是供审查的代码!

用户控制:

<asp:DropDownList ID="subjectNameDropDown" runat="server"/>
<asp:DropDownList ID="yearLevelDropDown" runat="server"/>

自动生成设计的代码隐藏:

public partial class NewSiteMetadataUserControl {
    protected global::System.Web.UI.WebControls.DropDownList subjectNameDropDown;
    protected global::System.Web.UI.WebControls.DropDownList yearLevelDropDown;
}

代码隐藏:

public partial class NewSiteMetadataUserControl : UserControl
{
    protected override void CreateChildControls()
    {
        subjectNameDropDown = new DropDownList();
        yearLevelDropDown = new DropDownList();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        EnsureChildControls();

        // Attempt 1
        List<String> subjectNames = GetSubjectValues();
        foreach (var subjectName in subjectNames)
            subjectNameDropDown.Items.Add(subjectName);
        subjectNameDropDown.DataBind();

        // Attempt 2
        List<String> yearLevels = GetYearLevelValues();
        yearLevelDropDown.DataSource = yearLevels;
        yearLevelDropDown.DataBind();
    }
}

这种方法应该有效吗?

如果有效应该,如何调试代码执行后发生的情况?

I'm trying to bind a List<String> to a DropDownList in a user control. I think I'm doing the right thing, but it seems that after my code executes the bindings are cleared. Here's the code for review!

User control:

<asp:DropDownList ID="subjectNameDropDown" runat="server"/>
<asp:DropDownList ID="yearLevelDropDown" runat="server"/>

Auto-generated designed code-behind:

public partial class NewSiteMetadataUserControl {
    protected global::System.Web.UI.WebControls.DropDownList subjectNameDropDown;
    protected global::System.Web.UI.WebControls.DropDownList yearLevelDropDown;
}

Code-behind:

public partial class NewSiteMetadataUserControl : UserControl
{
    protected override void CreateChildControls()
    {
        subjectNameDropDown = new DropDownList();
        yearLevelDropDown = new DropDownList();
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        EnsureChildControls();

        // Attempt 1
        List<String> subjectNames = GetSubjectValues();
        foreach (var subjectName in subjectNames)
            subjectNameDropDown.Items.Add(subjectName);
        subjectNameDropDown.DataBind();

        // Attempt 2
        List<String> yearLevels = GetYearLevelValues();
        yearLevelDropDown.DataSource = yearLevels;
        yearLevelDropDown.DataBind();
    }
}

Should this approach work?

If it should, how can I debug what happens after the code executes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千紇 2024-10-05 16:45:13

是的,这种方法应该可行,这就是为什么目前不行,

  • 使用 DataBind 完成的 DropDownList 需要 DataSource。这就是为什么尝试 #1 不起作用。
  • 如果您要绑定到 List,则没有明确的键/值对可供绑定。这就是为什么当绑定到 List(例如)时,您需要重写 Person 类中的 .ToString() 来提供键/值绑定,或手动设置DataTextFieldDataValueField
  • ASP.NET 无法计算出字符串 的键/值对。

考虑一下您想要什么 HTML。简单字符串的键/值应该是什么?这样做没有意义。

由于您并不真正关心“键”(只关心显示的内容),因此我建议您改为绑定到 Dictionary

要么让你的方法返回该值,要么迭代列表并将它们添加到带有索引的字典中。

Yes, this approach should work, here's why it currently isn't,

  • A DropDownList done with DataBind needs a DataSource. This is why Attempt #1 is not working.
  • If you're binding to a List<string>, there is no clear key/value pair to bind to. This is why when binding to a List<Person> (for example), you need to override .ToString() in the Person class to provide the key/value binding, or manually set the DataTextField, DataValueField.
  • There is no way for ASP.NET to work out a key/value pair for a string.

Think about what HTML you want. What should be the key/value for a simple string? Doesn't make sense does it.

Since you don't really care about the "key" (only what is displayed), i suggest you bind to a Dictionary<TKey,TValue> instead.

Either make your method return that, or iterate through the list and add them to the dictionary with an index.

似最初 2024-10-05 16:45:13

这里的问题是CreateChildControls。在我尝试完成这项工作的过程中,我添加了这个初始化控件的方法。这是不必要的,实际上会导致数据绑定被清除,因为它是在 OnLoad 之后由框架自动调用的。

解决方案是删除此方法以及对 EnsureChildControls 的调用。

The problem here was CreateChildControls. Somewhere in my attempts to make this work I added this method that initialises the controls. This isn't necessary and in fact caused the data bindings to be wiped out, as it was automatically called by the framework after OnLoad.

The solution was to remove this method and the call to EnsureChildControls.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文