如何使用List<ListItem>填充DropDownList

4

我有一个下拉列表。

我需要用 List<ListItem> 中收集的项目来填充它。

在我的脚本中,收集器已经被正确地填充了。

但是我无法填充下拉列表。我收到一个错误:

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();

有什么想法吗?谢谢。
8个回答

4
当您初始化listItem对象时,实际上是初始化属性(text,value)。
EX ( new ListItem(myUser.User (Text PROPERTY), myUser.UserId.ToString() (Value PROPERTY) )

尝试将其绑定:
    <asp:DropDownList ID="uxListUsers" runat="server" DataTextField="Text" 
DataValueField="Value">    

下拉菜单将获取存储在ListItem对象中的文本和值属性,并在用户界面中显示。

3
正如错误提示所述,System.Web.UI.WebControls.ListItem没有UserName成员。它确实有Text和Value成员。请尝试以下操作:
<asp:DropDownList ID="uxListUsers" runat="server" DataTextField="Text" DataValueField="Value">

3

看起来你正在绑定一个ListItem对象列表,它们不公开UserName或UserId属性。只需清除这些属性(DataTextField和DataValueField),你就可以轻松实现了。

或者更好的方法是,直接将你创建的列表项添加到下拉框中,跳过绑定的步骤。


2

很简单,只需使用ListItemCollection而不是通用列表List 这是它:

更改您的第一行

<asp:DropDownList ID="uxListUsers" runat="server">

ListItemCollection myListUsersInRoles = new ListItemCollection();
        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();

这解决了我的问题:<asp:TextBox ID =“uxGuidJobDisplayer”runat =“server”EnableViewState =“False”Enabled =“false” TextMode =“MultiLine”> </ asp:TextBox> - GibboK

1
不要将 ListItems 绑定到列表中,只需添加它们即可。绑定是使用 ListControl 的内置函数将对象转换为 ListItems 的一种方式。既然已经完成了转换,请节省自己和服务器的工作量,使用 ClearItems.AddRange 而不是 DataSourceDataBind
uxListUsers.Clear();
uxListUsers.Items.AddRange(myListUsersInRoles;)

您还需要从aspx中删除绑定指令:

<asp:DropDownList ID="uxListUsers" runat="server" />

为什么不使用ListItemCollection呢? - Waqas Raja
我的意思是,为什么你忽略了ListItemCollection?而使用通用的List,导致了uxListUsers.Clear()的头疼(删除项)。 - Waqas Raja
@Waqas Raja 解决这个问题有许多不同的方式。将其更改为ListItemCollection既无法减少代码行数也无法提高代码清晰度,因此我不确定为什么它应该被优先选择而不是简单地作为另一种选择。另一个可能更容易(需要更少的代码行更改)的方法是将用户添加到myListUsersInRoles中,并让DataBind处理ListItems(例如myListUsersInRoles.Add(myUser);)。我还可以想到几种解决方法,但我不知道它们中的任何一种是否更可取。 - jball
正如其名称所示,ListItem Collection非常适合使用通用列表来生成清晰的代码。干杯! - Waqas Raja

0
ListItem LI = new ListItem();
LI.Value = Convert.ToString("value");
LI.Text = Convert.ToString("text");
DrpDwnlist.Items.Add(LI);

0

可能最简单的方法是使用反射

uxListUsers.DataSource = typeof(ListItem).GetProperties();
uxListUsers.DataTextField = "Name"; //This is name of property enumerable typeof(ListItem).GetProperties().AsEnumerable().Select(p => new { p.Name }) 
uxListUsers.DataBind();

0

在通用列表中没有用户名和用户ID。如果命名很重要,你最好创建一个包含你需要绑定的两个字段的对象,而不是绑定到ListItem。如果必须绑定到ListItem,请考虑将Value和Text作为绑定点。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接