如何填充使用列表℃的DropDownList的;列表项> [英] How to populate a DropDownList using a List<ListItem>

查看:132
本文介绍了如何填充使用列表℃的DropDownList的;列表项>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DropDownList。

I have a DropDownList.

我需要在收集的项目填充一个列表与LT;列表项方式>

I need populate it with item collected in a List<ListItem>.

在我的剧本,收藏家已经正确填写。

In my script, collector has been populated properly.

但我不能填充的DropDownList。我收到一个错误:

But I cannot populate the DropDownList. I receive an error:

DataBinding: 'System.Web.UI.WebControls.ListItem' does not contain a property with the name 'UserName'."}


<asp:DropDownList ID="uxListUsers" runat="server" DataTextField="UserName" 
DataValueField="UserId">


List<ListItem> myListUsersInRoles = new List<ListItem>();
foreach (aspnet_Users myUser in context.aspnet_Users)
{
    // Use of navigation Property EntitySet
    if (myUser.aspnet_Roles.Any(r => r.RoleName == "CMS-AUTHOR" || r.RoleName == "CMS-EDITOR"))
        myListUsersInRoles.Add(new ListItem(myUser.UserName.ToString(), myUser.UserId.ToString()));
}
uxListUsers.DataSource = myListUsersInRoles; // MAYBE PROBLEM HERE????
uxListUsers.DataBind();

任何想法?谢谢

推荐答案

在初始化ListItem对象你实际初始化属性(文字,值)

When you init the listItem Object you actually init the properties (text,value)

EX ( new ListItem(myUser.User (Text PROPERTY), myUser.UserId.ToString() (Value PROPERTY) )

尝试绑定与

    <asp:DropDownList ID="uxListUsers" runat="server" DataTextField="Text" 
DataValueField="Value">    

下拉将采取Text和Value属性而存储在列表项对象

和显示它在用户界面

这篇关于如何填充使用列表℃的DropDownList的;列表项&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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