我尝试在 GridView 中使用 DropDownList 设置“RoleName”,但出现此错误:
DataBinding: 'System.String' does not contain a property with the name 'RoleName'.
我做了你想象的任何事情,在不同的论坛上提到过。以下是代码:
<asp:GridView ID="gvUsers" runat="server" PageSize="25" CssClass="gv" AutoGenerateColumns="False" DataKeyNames="UserName" DataSourceID="odsUsersInRoles" EnableModelValidation="True">
<Columns>
<asp:TemplateField HeaderText="Role">
<EditItemTemplate>
<asp:DropDownList ID="ddlRoles" runat="server" DataSourceID="odsRoles" DataTextField="RoleName" DataValueField="RoleName" SelectedValue='<%# Eval("RoleName") %>'></asp:DropDownList>
<asp:ObjectDataSource ID="odsRoles" runat="server" SelectMethod="GetAllRolesButAdministrator" TypeName="cUsers"></asp:ObjectDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("RoleName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
public static string[] GetAllRolesButAdministrator()
{
string[] roles = System.Web.Security.Roles.GetAllRoles();
int removeIndex = Array.IndexOf(roles, "Administrator");
if (removeIndex >= 0)
{
string[] newStrItems = new string[roles.Length - 1];
for (int i = 0, j = 0; i < newStrItems.Length; i++, j++)
{
if (i == removeIndex)
j++;
newStrItems[i] = roles[j];
}
roles = newStrItems;
}
return roles;
}
请有人帮助我!!!