绑定数组 +下拉列表中的一个对象
我有一个 Person 类型的数组,其中包含字符串对象:FirstName、LastName、login。
我将其绑定到下拉列表。
现在,除了这个数组之外,我还想显示另一个名为“Desk”的项目。我怎样才能做到这一点?
我当前的代码是:
Person[] traders = GetTraders();
ddl_trader.Items.Clear();
ddl_trader.DataSource = traders;
ddl_trader.DataTextField = "LastName";
ddl_trader.DataValueField = "Login";
ddl_trader.DataBind();
我还希望我添加的一项额外项目成为默认选定项目。
I have an array of type Person, which contains string objects: FirstName, LastName, login.
I have this bound to a dropdownlist.
Now along with this array, I also want to display one more item called "Desk". How can i do that?
My current code is:
Person[] traders = GetTraders();
ddl_trader.Items.Clear();
ddl_trader.DataSource = traders;
ddl_trader.DataTextField = "LastName";
ddl_trader.DataValueField = "Login";
ddl_trader.DataBind();
I also want that one extra item I'm adding to be the default selected item.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
AppendDataBoundItems
属性设置为true
(默认情况下为false
),手动添加项目,然后执行数据绑定过程以添加剩余的项目项目。AppendDataBoundItems
确定在数据绑定期间是否清除列表。如果您需要在绑定列表后添加新项目,则可以执行
此操作,不需要将
AppendDataBoundItems
设置为true
。You can set the
AppendDataBoundItems
property totrue
(it'sfalse
by default), add your item manually, then do the databinding process to add the remaining items.AppendDataBoundItems
determines if the list is cleared during databinding or not.if you need to add the new item after the list has been bound, you can do
this does not require setting
AppendDataBoundItems
totrue
.其中一种方法是将
GetTraders()
的结果加载到List
中。然后,您可以将一个或多个新的Person
对象添加到列表中,然后再将其绑定到下拉列表。您还可以定义一个附加数组并将其连接到结果,而不是创建组合集合。 (注意:这将是两个不同的数组,被视为单个序列,但支持数组仍然是独立的)。
One such method is to load the result of
GetTraders()
into aList<Person>
. You can then add one or many newPerson
objects to the list before binding it to your dropdown.You could also define an additional array and concatenate that to the result instead of creating a combined collection. (Note: this would be two distinct arrays treated as a single sequence, however the backing arrays would still be seperate).