将空项目添加到 C# 中自定义对象的下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/679128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:08:39  来源:igfitidea点击:

Add empty item to dropdownlist of custom objects in C#

c#asp.net

提问by Jeffrey Cameron

We are binding a list of custom objects to an ASP.NET DropDownList in C# but we want to allow for the DropDownList to not have anything selected initially. One way of doing this might be to create an intermediate list of strings, populate the first one with an empty string and then populate the rest of the list with the custom object information.

我们正在将自定义对象列表绑定到 C# 中的 ASP.NET DropDownList,但我们希望允许 DropDownList 最初没有选择任何内容。执行此操作的一种方法可能是创建一个中间字符串列表,使用空字符串填充第一个列表,然后使用自定义对象信息填充列表的其余部分。

This doesn't seem very elegant however, does anyone have a better suggestion?

然而,这似乎不是很优雅,有人有更好的建议吗?

采纳答案by Sean Bright

Yes, create your list like so:

是的,像这样创建你的列表:

<asp:DropDownList ID="Whatever" runat="server" AppendDataBoundItems="True">
    <asp:ListItem Value="" Text="Select one..." />
</asp:DropDownList>

(Note the use of AppendDataBoundItems="True")

(注意使用AppendDataBoundItems="True"

And then when you bind, the bound items are put after the empty item rather than replacing it.

然后当你绑定时,绑定的项目会放在空项目之后而不是替换它。

回答by Richard

You could add to the databound event:

您可以添加到数据绑定事件:

protected void DropDownList1_DataBound(object sender, EventArgs e)
        {
            DropDownList1.Items.Insert(0,new ListItem("",""));
        }

回答by George Mauer

Just working on this actually, here's what I got so far (along with a couple databinding goodies)

实际上只是在研究这个,这是我到目前为止所得到的(以及一些数据绑定的好东西)

public interface ICanBindToObjectsKeyValuePair {
    void BindToProperties<TYPE_TO_BIND_TO>(IEnumerable<TYPE_TO_BIND_TO> bindableEnumerable, Expression<Func<TYPE_TO_BIND_TO, object>> textProperty, Expression<Func<TYPE_TO_BIND_TO, object>> valueProperty);
}

public class EasyBinderDropDown : DropDownList, ICanBindToObjectsKeyValuePair {
    public EasyBinderDropDown() {
        base.AppendDataBoundItems = true;
    }
    public void BindToProperties<TYPE_TO_BIND_TO>(IEnumerable<TYPE_TO_BIND_TO> bindableEnumerable,
        Expression<Func<TYPE_TO_BIND_TO, object>> textProperty, Expression<Func<TYPE_TO_BIND_TO, object>> valueProperty) {
        if (ShowSelectionPrompt)
            Items.Add(new ListItem(SelectionPromptText, SelectionPromptValue));
        base.DataTextField = textProperty.MemberName();
        base.DataValueField = valueProperty.MemberName();
        base.DataSource = bindableEnumerable;
        base.DataBind();
    }
    public bool ShowSelectionPrompt { get; set; }
    public string SelectionPromptText { get; set; }
    public string SelectionPromptValue { get; set; }
    public virtual IEnumerable<ListItem> ListItems {
        get { return Items.Cast<ListItem>(); }
    }
}

Notice one thing that you can do is

请注意,您可以做的一件事是

dropDown.BindToProperties(myCustomers, c=>c.CustomerName, c=>c.Id);

回答by soaringCelia

First:

第一的:

DropDownList1.Items.Clear();

DropDownList1.Items.Clear();

Then add listItems to the dropDownList.

然后将 listItems 添加到 dropDownList。

This prevents the dropDownList from acquiring an ever increasing list of items every time it is rendered in a postback or asynchronous postback.

这可以防止 dropDownList 每次在回发或异步回发中呈现时获取不断增加的项目列表。