请建议 -
CheckBoxList 不可见 - 它必须显示 3 个角色(目前存在):
<%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="false" CodeBehind="admin.aspx.cs"
Inherits="HSE_Monitoring_Application.admin.admin" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server">
<h3>Role Manager</h3>
<div>
Enter UserName:
<asp:TextBox ID="TxtUserName" runat="server"></asp:TextBox>
<asp:Button ID="LookupBtn" runat="server" Text="Search" />
</div>
<div class="roleList">
<asp:CheckBoxList ID="RoleList" runat="server" Visible=true>
</asp:CheckBoxList>
</div>
<div>
<asp:button ID="UpdateBtn" text="Update" Visible="false" runat="server" />
</div>
</asp:Content>
结果 - 它必须显示 3 个具有 3 个角色的复选框 - 管理员、审批者、审核员和按钮“更新”。这些角色存在。借助角色复选框控件 - 管理员可以在页面上管理角色成员资格。
它仅显示带有文本框和按钮(搜索)的标签(输入用户名)。请回复。如有必要,我可以提供所需的文件。我花了很多时间来解决这个问题。
带着敬意!
'admin.aspx.cs' 的内容是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace HSE_Monitoring_Application.admin
{
public partial class admin : System.Web.UI.Page
{
private void PopulateRoleList(String userName)
{
RoleList.Items.Clear();
string[] roleNames;
//string roleName="";
roleNames = Roles.GetAllRoles();
Response.Write(roleNames);
foreach (string roleName in roleNames)
{
ListItem roleListItem = new ListItem();
roleListItem.Text = roleName;
roleListItem.Selected = Roles.IsUserInRole(userName, roleName);
RoleList.Items.Add(roleListItem);
}
}
private void UpdateRolesFromList()
{
foreach (ListItem roleListItem in RoleList.Items)
{
string roleName = roleListItem.Value;
string userName = TxtUserName.Text;
Boolean enableRole = roleListItem.Selected;
if (enableRole == true && Roles.IsUserInRole(userName, roleName) == false)
{
Roles.AddUserToRole(userName, roleName);
}
else if (enableRole == false && Roles.IsUserInRole(userName, roleName) == true)
{
Roles.RemoveUserFromRole(userName, roleName);
}
}
}
private void LookupBtn_Click(object sender, EventArgs e)
{
PopulateRoleList(TxtUserName.Text);
UpdateBtn.Visible = true;
}
private void UpdateBtn_Click(object sender, EventArgs e)
{
UpdateRolesFromList();
PopulateRoleList(TxtUserName.Text);
}
}
}