空项自定义对象的DropDownList添加在C# [英] Add empty item to dropdownlist of custom objects in C#

查看:113
本文介绍了空项自定义对象的DropDownList添加在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们是有约束力的自定义对象到ASP.NET的DropDownList在C#中的列表,但我们希望允许的DropDownList来没有什么初步选定。这样做的一种方式可能是创建字符串的中间列表,填充第一个用空字符串,然后填充自定义对象信息的列表的其余部分。

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.

这似乎并不很优雅但是,没有任何人有一个更好的建议?

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

推荐答案

是,创建您的列表如下所示:

Yes, create your list like so:

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

(注意使用 AppendDataBoundItems =真

(Note the use of AppendDataBoundItems="True")

然后当你绑定,绑定的物品的空项目后放,而不是取代它。

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

这篇关于空项自定义对象的DropDownList添加在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆