I have a DropDownList.
I need populate it with item collected in a List<ListItem>.
In my script, collector has been populated properly.
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();
Any ideas? Thanks
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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) )
try to bind that with
<asp:DropDownList ID="uxListUsers" runat="server" DataTextField="Text" DataValueField="Value">
the dropdown will take the Text and Value Properties which store in the ListItem Object
and show it in the user interface
Method 2
It looks like you’re binding to a list of ListItem objects, which don’t expose a UserName or UserId property. Just clear those properties (DataTextField and DataValueField) and you should be good to go.
Or better yet, just add teh list items that you’ve created to the the drop down directly and skip binding.
Method 3
As the error says, System.Web.UI.WebControls.ListItem has no UserName member. It does have Text and Value members. Try the following
<asp:DropDownList ID="uxListUsers" runat="server" DataTextField="Text" DataValueField="Value">
Method 4
its so simple just use ListItemCollection instead of generic list List
here it is
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();
Method 5
Don’t bind ListItems to a list, simply add them. Binding is a way of using the built in functions of a ListControl to convert objects into ListItems. Since you’ve already done the conversion, save yourself and the server some work and use Clear and Items.AddRange instead of DataSource and DataBind:
uxListUsers.Clear(); uxListUsers.Items.AddRange(myListUsersInRoles;)
You’ll also need to remove the binding directives from the aspx:
<asp:DropDownList ID="uxListUsers" runat="server" />
Method 6
There is no Username and UserId in a generic list. You Your best bet, if the naming is important, is create an object that contains the two fields you need to bind to rather than bind to ListItem. If you must bind to ListItem, consider Value and Text as binding points.
Method 7
May be simplest way it’s 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();
Method 8
ListItem LI = new ListItem();
LI.Value = Convert.ToString("value");
LI.Text = Convert.ToString("text");
DrpDwnlist.Items.Add(LI);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0