Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用列表填充下拉列表<;列表项>;_C#_Asp.net - Fatal编程技术网

C# 如何使用列表填充下拉列表<;列表项>;

C# 如何使用列表填充下拉列表<;列表项>;,c#,asp.net,C#,Asp.net,我有一个下拉列表 我需要用列表中收集的项目填充它 在我的脚本中,收集器已正确填充 但我无法填充DropDownList。我收到一个错误: DataBinding: 'System.Web.UI.WebControls.ListItem' does not contain a property with the name 'UserName'."} List myListUsersInRoles=new List(); foreach(上下文中的aspnet\u用户myUser.aspn

我有一个下拉列表

我需要用
列表中收集的项目填充它

在我的脚本中,收集器已正确填充

但我无法填充DropDownList。我收到一个错误:

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



List myListUsersInRoles=new List();
foreach(上下文中的aspnet\u用户myUser.aspnet\u用户)
{
//导航属性EntitySet的使用
if(myUser.aspnet_Roles.Any(r=>r.RoleName==“CMS-AUTHOR”| r.RoleName==“CMS-EDITOR”))
添加(新列表项(myUser.UserName.ToString(),myUser.UserId.ToString());
}
uxListUsers.DataSource=myListUsersInRoles;//也许这里有问题????
uxListUsers.DataBind();

有什么想法吗?谢谢

看起来您正在绑定到ListItem对象列表,这些对象不公开用户名或用户ID属性。只需清除这些属性(DataTextField和DataValueField),就可以开始了


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

如错误所示,System.Web.UI.WebControl.ListItem没有用户名成员。它确实有文本和值成员。试试下面的方法

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


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

< P>不要将<代码> ListTimes < /代码>绑定到列表中,只需添加它们即可。绑定是一种使用
ListControl
的内置函数将对象转换为
ListItems
的方法。由于您已经完成了转换,请保存您自己和服务器的一些工作,并使用
Clear
项。AddRange
而不是
DataSource
DataBind

uxListUsers.Clear();
uxListUsers.Items.AddRange(myListUsersInRoles;)
您还需要从aspx中删除绑定指令:

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

它非常简单,只需使用它而不是通用列表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();

ListItemCollection myListUsersInRoles=新ListItemCollection();
foreach(上下文中的aspnet\u用户myUser.aspnet\u用户)
{
//导航属性EntitySet的使用
if(myUser.aspnet_Roles.Any(r=>r.RoleName==“CMS-AUTHOR”| r.RoleName==“CMS-EDITOR”))
添加(新列表项(myUser.UserName.ToString(),myUser.UserId.ToString());
}
uxListUsers.DataSource=myListUsersInRoles;//也许这里有问题????
uxListUsers.DataBind();

初始化listItem对象时,实际上是初始化属性(文本、值)

试着用绳子把它绑起来

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

下拉列表将获取存储在
ListItem
对象中的文本和值属性


并在用户界面中显示它

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

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

为什么不使用ListItemCollection呢?我的意思是,为什么忽略ListItemCollection?(更喜欢或)使用泛型列表,这让uxListUsers头疼不已;(移除物品)@Waqas Raja有很多不同的方法来解决这个问题。更改为
ListItemCollection
既不能减少代码行数,也不能提高代码的清晰度,因此我不确定为什么它应该是首选,而不仅仅是一种替代方案。OP的另一个可能更简单(更改代码行更少)的方法是简单地将用户添加到
myListUsersInRoles
,并让
DataBind
ListItems
进行处理(例如
myListUsersInRoles.add(myUser);
)。我还可以想出一些其他的方法来修复它,但我不知道它们中有哪一种更可取。正如名字解释的ListItem集合一样,使用generic list来生成清晰的代码非常好。这可以解决我的问题
EX ( new ListItem(myUser.User (Text PROPERTY), myUser.UserId.ToString() (Value PROPERTY) )
    <asp:DropDownList ID="uxListUsers" runat="server" DataTextField="Text" 
DataValueField="Value">    
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();
ListItem LI = new ListItem();
LI.Value = Convert.ToString("value");
LI.Text = Convert.ToString("text");
DrpDwnlist.Items.Add(LI);