从数据列表(C#/asp.net)修改复选框
我正在尝试修改此asp.net/C代码。该页面允许管理员添加用户,并从复选框列表中选择该用户将具有的角色。有11个复选框,旁边有角色可供选择。我想整理这些复选框。我想把11个复选框分为3组CMS、LMS和Admin,以便在页面上将它们分为三个部分。我只想为CMS组选择一个复选框。CMS组可以是一个下拉框,如果这会使它更容易。而且,当用户将鼠标悬停在这些角色上时,所有这些角色都必须有工具提示 您将在代码中看到,复选框是使用userRolesDataSource创建的。此数据源使用名为“GetUserRoles”的方法,该方法有两个参数“username”和“onlyncludeSystemAdminRole”布尔值。选择方法检查用户名是否为系统管理员,如果为真,则排除所有其他角色选择 这是新的GetUserRoles方法:从数据列表(C#/asp.net)修改复选框,c#,sql,asp.net,C#,Sql,Asp.net,我正在尝试修改此asp.net/C代码。该页面允许管理员添加用户,并从复选框列表中选择该用户将具有的角色。有11个复选框,旁边有角色可供选择。我想整理这些复选框。我想把11个复选框分为3组CMS、LMS和Admin,以便在页面上将它们分为三个部分。我只想为CMS组选择一个复选框。CMS组可以是一个下拉框,如果这会使它更容易。而且,当用户将鼠标悬停在这些角色上时,所有这些角色都必须有工具提示 您将在代码中看到,复选框是使用userRolesDataSource创建的。此数据源使用名为“GetUse
public List<Pair<string, bool>> GetUserRoles(
string userName, bool onlyIncludeSystemAdminRole)
{
// Return a list of "role name"/"has role" pairs for the given user.
// If the "onlyIncludeSystemAdminRole" flag is true, only return the
// "System Admin" role, otherwise return all roles other than
// "System Admin".
List<Pair<string, bool>> userRolesList = null;
if (!onlyIncludeSystemAdminRole)
{
string[] allRoles = Roles.GetAllRoles();
HashSet<string> rolesForUser = null;
if (!String.IsNullOrWhiteSpace(userName))
{
rolesForUser = new HashSet<string>(
Roles.GetRolesForUser(userName));
}
userRolesList = (from roleName in allRoles
where roleName != RoleNames.SYSTEM_ADMIN
select new Pair<string, bool>()
{
First = roleName,
Second = rolesForUser != null
? rolesForUser.Contains(roleName)
: false
}).ToList();
}
else
{
userRolesList = new List<Pair<string, bool>>()
{
new Pair<string, bool>(
RoleNames.SYSTEM_ADMIN,
Roles.IsUserInRole(userName, RoleNames.SYSTEM_ADMIN))
};
}
return userRolesList;
}
公共列表GetUserRoles(
字符串用户名,bool onlyIncludeSystemAdminRole)
{
//返回给定用户的“角色名称”/“具有角色”对列表。
//如果“onlyIncludeSystemAdminRole”标志为true,则仅返回
//“系统管理员”角色,否则返回除
//“系统管理员”。
List userRolesList=null;
if(!onlyIncludeSystemAdminRole)
{
字符串[]allRoles=Roles.GetAllRoles();
HashSet rolesForUser=null;
如果(!String.IsNullOrWhiteSpace(用户名))
{
rolesForUser=新哈希集(
Roles.GetRolesForUser(用户名));
}
userRolesList=(来自allRoles中的roleName)
其中roleName!=RoleNames.SYSTEM\u ADMIN
选择新对()
{
第一个=roleName,
Second=rolesForUser!=null
?rolesForUser.Contains(roleName)
:错
}).ToList();
}
其他的
{
userRolesList=新列表()
{
新的一对(
RoleNames.SYSTEM_ADMIN,
Roles.IsUserInRole(用户名,RoleNames.SYSTEM\u ADMIN))
};
}
返回用户角色列表;
}
类似的东西仍然需要在所有的复选框上运行
为了解决这个问题,我计划使用类似的方法。可以将角色列表作为参数提供给它。并且,它将排除提供的列表,并每隔一个角色返回一次
public IEnumerable<string> GetRoles(params string[] roleNamesToExclude)
{
var query = Uow.Roles
.AsQueryable()
.Select(r => r.RoleName);
if (roleNamesToExclude != null && roleNamesToExclude.Any())
{
query = query
.Where(r => !roleNamesToExclude.Contains(r));
}
return query.ToList();
}
public IEnumerable GetRoles(参数字符串[]RoleNameStoreExclude)
{
var query=Uow.Roles
.AsQueryable()
.选择(r=>r.RoleName);
if(roleNamesToExclude!=null&&roleNamesToExclude.Any())
{
查询=查询
其中(r=>!roleNamesToExclude.Contains(r));
}
返回query.ToList();
}
我重建它的想法是用3个新的数据源替换userRolesDataSource:CMSRolesDataSource、LMSRolesDataSource、AdminRolesDataSource。然后创建一个新方法,该方法采用username、systemadmin boolean以及类似于上述方法的可排除角色列表
我对编码还是相当陌生,我想知道是否有一种更简单的方法来完成所有这些。asp和c#的代码如下。提前谢谢
asp
*
*
C#
受保护的无效页面加载(对象发送方,事件参数e)
{
如果(!IsPostBack)
{
if(SessionWrapper.ActiveUserID==null)
{
userFormView.ChangeMode(FormViewMode.Insert);
}
}
}
受保护的void detailDataSource\u插入(
对象发送方,对象数据源MethodEventArgs(e)
{
DataList userRolesDataList=(DataList)userFormView.Row.FindControl(
“用户角色数据列表”);
列表角色=GetSelectedUserRoles(userRolesDataList);
if(SessionWrapper.WorkingOrganizationId!=null)
{
e、 InputParameters[“organizationId”]
=SessionWrapper.WorkingOrganizationId;
}
其他的
{
e、 InputParameters[“organizationId”]
=SessionWrapper.ActiveOrganizationID;
}
e、 InputParameters[“acceptedAgreement”]=false;
e、 InputParameters[“changedPassword”]=false;
e、 InputParameters[“mustUpdateProfile”]=true;
<asp:ObjectDataSource ID="userRolesDataSource" runat="server" OnSelecting="userRolesDataSource_Selecting" OldValuesParameterFormatString="{0}" SelectMethod="GetUserRoles" TypeName="VirtualExpertClinics.AutismProClassroom.Data.UserBLL">
<SelectParameters>
<asp:Parameter Name="userName" Type="String" />
<asp:Parameter Name="onlyIncludeSystemAdminRole" Type="Boolean" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="titlesDataSource" runat="server"
OldValuesParameterFormatString="{0}" SelectMethod="GetOrganizationTitles"
TypeName="VirtualExpertClinics.AutismProClassroom.Data.OrganizationBLL"
OnSelecting="titlesDataSource_Selecting">
<SelectParameters>
<asp:Parameter Name="organizationId" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<h5><%: Resources.Global.UserRoles %></h5>
<asp:DataList ID="userRolesDataList" runat="server" CssClass="formTable" DataSourceID="userRolesDataSource" Enabled="false">
<ItemTemplate>
<asp:CheckBox ID="userRoleCheckBox" runat="server" Text='<%# Eval("First") %>' Checked='<%# Eval("Second") %>' />
</ItemTemplate>
</asp:DataList>
</ItemTemplate>
<EditItemTemplate>
<asp:Panel ID="userDetailsPanel" runat="server" DefaultButton="saveButton">
<table class="formTable extraCellPadding">
<tr>
<td class="alignTop">
<asp:Label ID="userRolesLabel" runat="server" Text="<%$ Resources:Global, UserRoles %>"></asp:Label>
</td>
<td>
<asp:CustomValidator ID="userRolesCustomValidator" runat="server" ValidationGroup="validationGroup"
OnServerValidate="userRolesCustomValidator_ServerValidate" Display="Dynamic">*</asp:CustomValidator>
<asp:DataList ID="userRolesDataList" runat="server" CssClass="formTable" OnItemDataBound="userRolesDataList_ItemDataBound" DataSourceID="userRolesDataSource">
<ItemTemplate>
<asp:CheckBox ID="userRoleCheckBox" runat="server" Text='<%# Eval("First") %>' Checked='<%# Eval("Second") %>' />
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
<div style="margin-top: 1em;">
<asp:Button ID="saveButton" runat="server" CssClass="btn" ValidationGroup="validationGroup" CommandName="Update" Text="<%$ Resources:Global, Save %>" />
<asp:Button ID="cancelButton" runat="server" CssClass="btn" CommandName="Cancel" Text="<%$ Resources:Global, Cancel %>" />
</div>
</asp:Panel>
</EditItemTemplate>
<InsertItemTemplate>
<asp:Panel ID="userDetailsPanel" runat="server" DefaultButton="saveButton">
<table class="formTable extraCellPadding">
<tr>
<td class="alignTop">
<asp:Label ID="userRolesLabel" runat="server" CssClass="label" Text="<%$ Resources:Global, UserRoles %>"></asp:Label>
</td>
<td>
<asp:CustomValidator ID="userRolesCustomValidator" runat="server" ValidationGroup="validationGroup"
OnServerValidate="userRolesCustomValidator_ServerValidate" Display="Dynamic">*</asp:CustomValidator>
<asp:DataList ID="userRolesDataList" runat="server" CssClass="formTable" OnItemDataBound="userRolesDataList_ItemDataBound" DataSourceID="userRolesDataSource">
<ItemTemplate>
<asp:CheckBox ID="userRoleCheckBox" runat="server" Text='<%# Eval("First") %>' Checked='<%# Eval("Second") %>' />
</ItemTemplate>
</asp:DataList>
</td>
</tr>
</table>
<div style="margin-top: 1em;">
<asp:Button ID="saveButton" runat="server" CssClass="btn" ValidationGroup="validationGroup" CommandName="Insert" Text="<%$ Resources:Global, Save %>" />
<asp:Button ID="cancelButton" runat="server" CssClass="btn" CommandName="Cancel" Text="<%$ Resources:Global, Cancel %>" />
</div>
</asp:Panel>
</InsertItemTemplate>
</asp:FormView>
</div>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (SessionWrapper.ActiveUserID == null)
{
userFormView.ChangeMode(FormViewMode.Insert);
}
}
}
protected void detailDataSource_Inserting(
object sender, ObjectDataSourceMethodEventArgs e)
{
DataList userRolesDataList = (DataList)userFormView.Row.FindControl(
"userRolesDataList");
List<string> roles = GetSelectedUserRoles(userRolesDataList);
if (SessionWrapper.WorkingOrganizationId != null)
{
e.InputParameters["organizationId"]
= SessionWrapper.WorkingOrganizationId;
}
else
{
e.InputParameters["organizationId"]
= SessionWrapper.ActiveOrganizationID;
}
e.InputParameters["acceptedAgreement"] = false;
e.InputParameters["changedPassword"] = false;
e.InputParameters["mustUpdateProfile"] = true;
e.InputParameters["timeZoneId"] = TimeZoneInfo.Utc.Id;
e.InputParameters["roles"] = roles;
e.InputParameters["registrationCodeId"] = null;
e.InputParameters["createPasswordTicket"] = true;
MiscUtil.TrimInputParameters(e.InputParameters);
}
protected void detailDataSource_Updating(
object sender, ObjectDataSourceMethodEventArgs e)
{
DataList userRolesDataList = (DataList)userFormView.Row.FindControl(
"userRolesDataList");
e.InputParameters["roles"] = GetSelectedUserRoles(userRolesDataList);
UserProfileDS.UserProfileRow user = UserBLL.GetUserByUserID(
(Guid)userFormView.SelectedValue);
e.InputParameters["acceptedAgreement"] = user.AcceptedAgreement;
e.InputParameters["changedPassword"] = user.ChangedPassword;
e.InputParameters["mustUpdateProfile"] = user.MustUpdateProfile;
e.InputParameters["timeZoneId"] = user.TimeZoneId;
MiscUtil.TrimInputParameters(e.InputParameters);
}
protected void userRolesDataList_ItemDataBound(
object sender, DataListItemEventArgs e)
{
Pair<string, bool> rolePair = (Pair<string, bool>)e.Item.DataItem;
if (rolePair.First == RoleNames.CMS_OWNER
|| rolePair.First == RoleNames.CMS_DATA_ENTRY
|| rolePair.First == RoleNames.CMS_BROWSER)
{
// Disable the CMS roles if CMS access is not enabled for the
// current organization.
OrganizationDS.OrganizationRow organizationRow
= OrganizationBLL.GetOrganizationById(
SessionWrapper.ActiveOrganizationID);
if (!organizationRow.EnableCMS)
{
CheckBox userRoleCheckBox = (CheckBox)e.Item.FindControl(
"userRoleCheckBox");
userRoleCheckBox.Enabled = false;
userRoleCheckBox.ToolTip = Resources.Global.CMSNotEnabled;
}
}
else if (rolePair.First == RoleNames.WORKSHOPS_USER)
{
// Disable the "Workshops User" role if the corresponding
// check box is not selected and the current organization has
// reached their maximum number of learners.
if (!rolePair.Second
&& OrganizationBLL.HasReachedMaxLearners(
SessionWrapper.ActiveOrganizationID))
{
CheckBox userRoleCheckBox = (CheckBox)e.Item.FindControl(
"userRoleCheckBox");
userRoleCheckBox.Enabled = false;
userRoleCheckBox.ToolTip
= Resources.Global.OrganizationMaxLearnersReached;
}
}
else if (rolePair.First == RoleNames.LOCAL_SYSTEM_ADMIN)
{
// Disable the "Local System Admin" role if the current user
// is not a system admin.
if (!Roles.IsUserInRole(RoleNames.SYSTEM_ADMIN))
{
CheckBox userRoleCheckBox = (CheckBox)e.Item.FindControl(
"userRoleCheckBox");
userRoleCheckBox.Enabled = false;
}
}
}
protected void userRolesDataSource_Selecting(
object sender, ObjectDataSourceSelectingEventArgs e)
{
string userName;
bool onlyIncludeSystemAdminRole;
if (userFormView.CurrentMode == FormViewMode.Insert)
{
userName = "";
onlyIncludeSystemAdminRole
= (SessionWrapper.ActiveOrganizationID == 0);
}
else
{
UserProfileDS.UserProfileRow userRow
= (UserProfileDS.UserProfileRow)userFormView.DataItem;
userName = userRow.UserName;
onlyIncludeSystemAdminRole = Roles.IsUserInRole(
userRow.UserName, RoleNames.SYSTEM_ADMIN);
}
e.InputParameters["userName"] = userName;
e.InputParameters["onlyIncludeSystemAdminRole"]
= onlyIncludeSystemAdminRole;
}
private List<string> GetSelectedUserRoles(DataList userRolesDataList)
{
List<string> roles = new List<string>();
foreach (DataListItem item in userRolesDataList.Items)
{
CheckBox userRoleCheckBox = (CheckBox)item.FindControl(
"userRoleCheckBox");
if (userRoleCheckBox.Checked)
{
roles.Add(userRoleCheckBox.Text);
}
}
return roles;
}
protected void userRolesCustomValidator_ServerValidate(
object source, ServerValidateEventArgs args)
{
CustomValidator userRolesCustomValidator = (CustomValidator)source;
DataList userRolesDataList = (DataList)userFormView.Row.FindControl(
"userRolesDataList");
List<string> roles = GetSelectedUserRoles(userRolesDataList);
AdministrationUtil.ValidateUserRoleSelection(
roles, userRolesCustomValidator, args);
}
}