How to populate a DropDownList using List <ListItem>

I have a DropDownList.

I need to populate it with an element assembled in a List<ListItem> .

In my script, the collector was correctly populated.

But I can not fill in a DropDownList. I get an error message:

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

Any ideas? Thanks

+4
source share
8 answers

When you start the listItem object, you actually run the properties (text, value)

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

try linking this with

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

the drop-down menu will accept the text properties and values ​​that are stored in the ListItem Object

and show it in the user interface

+4
source

It looks like you are binding to a list of ListItem objects that do not display the UserName or UserId property. Just clear these properties (DataTextField and DataValueField) and you should be good to go.

Or even better, just add the list items you created to the dropdown directly and skip the binding.

+3
source

As the error says, System.Web.UI.WebControls.ListItem does not have a UserName member. It has members of Text and Value. Try the following

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

its so simple to use ListItemCollection instead of the general List List here

change your first line

 <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(); 
+2
source

Do not bind ListItems to a list, just add them. Binding is a way to use ListControl built-in functions to convert objects to ListItems . Since you've already done the conversion, save yourself and the server, and use Clear and Items.AddRange instead of DataSource and DataBind :

 uxListUsers.Clear(); uxListUsers.Items.AddRange(myListUsersInRoles;) 

You also need to remove the binding directives from aspx:

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

There is no username and UserId in the general list. Your best bet, if it's important to name it, is to create an object that contains two fields that need to be bound, not bound to a ListItem. If you must bind to a ListItem, consider the value and text as anchor points.

0
source

Maybe the easiest way to use reflection

 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
source
 ListItem LI = new ListItem(); LI.Value = Convert.ToString("value"); LI.Text = Convert.ToString("text"); DrpDwnlist.Items.Add(LI); 
0
source

Source: https://habr.com/ru/post/1337975/


All Articles