无法应用循环数据行的条件
我在数据行中应用了循环,我需要检查用户角色是否为管理员,我需要应用一些条件。
现在,一个用户可以拥有多个角色,比如用户 Smith 有 3 个角色:管理员、开发人员和测试人员。因此对于史密斯来说,条件成立了。对于 Jane 来说,她有 4 个角色:开发人员、测试人员、分析师和开发人员。普通用户,因此对于 jane 来说,条件变为 false(因为她不是管理员)
现在我已将代码编写为
// filling up the dataTable.
DataTable dtAssignedRoles = (DataTable (Session[GlobalConstants.SESSION_USER_ASSGN_ROLE_DT]);
if (dtAssignedRoles != null && dtAssignedRoles.Rows.Count > 0)
{
foreach (DataRow dr in dtAssignedRoles.Rows)
{
if (dr["OT_ROLE"].ToString().ToUpper().Equals("ADMIN"))
{
// apply condition for admin here!
}
}
}
// Condition that would execute for Jane
if (strICol.Equals("N"))
{
e.Row.Cells[0].Text = string.Empty;
e.Row.Cells[0].Controls.Clear();
Image imgIColumnDesc = new Image();
imgIColumnDesc.ImageUrl = "~/Images/blackcircle.png";
e.Row.Cells[0].Controls.Add(imgIColumnDesc);
}
问题:对于 smith,条件失败,因为尽管他是管理员,但他也是开发人员和测试人员。因此应用了 2 个条件;一个用于管理员,另一个用于非管理员(开发人员+测试人员)
因此我想,我需要检查所有行,如果有一个管理员角色,则应该满足条件。但我不知道该怎么做?
请指导。谢谢
I have apply looping in datarow and i need to check that if user role is admin, i need to apply some condition.
Now a user can have multiple roles say User Smith has 3 roles: Admin, Developer and Tester. Hence for smith the condition comes true. For Jane she has 4 roles: Developer, Tester, Analyst & Normal user, so for jane the condition become false (as she is not admin)
Now i have written code as
// filling up the dataTable.
DataTable dtAssignedRoles = (DataTable (Session[GlobalConstants.SESSION_USER_ASSGN_ROLE_DT]);
if (dtAssignedRoles != null && dtAssignedRoles.Rows.Count > 0)
{
foreach (DataRow dr in dtAssignedRoles.Rows)
{
if (dr["OT_ROLE"].ToString().ToUpper().Equals("ADMIN"))
{
// apply condition for admin here!
}
}
}
// Condition that would execute for Jane
if (strICol.Equals("N"))
{
e.Row.Cells[0].Text = string.Empty;
e.Row.Cells[0].Controls.Clear();
Image imgIColumnDesc = new Image();
imgIColumnDesc.ImageUrl = "~/Images/blackcircle.png";
e.Row.Cells[0].Controls.Add(imgIColumnDesc);
}
Problem: For smith the condition fails as although he is admin, he is also developer and tester. hence 2 conditions get applied; one for admin and another for non-admin (dev + tester)
Hence i guess, i need to check in all the rows and if there is one role with admin, the condition should be met. But i don't know how to do it?
Please guide. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用此辅助函数来确定用户是否具有特定角色...
编辑:或与 Linq
然后使用...
You can use this helper function to determine if a user has a particular role...
EDIT: OR with Linq
Then to use...
如果在应用管理员条件后添加语句
break;
,这将跳出循环并进入流程的下一步。If, after you apply the condition for the admin, you add the statement
break;
, this will break out of the loop and move onto the next step in the process.