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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With