如何显示代码隐藏页面上存在的已分配角色 [英] How do I display assigned roles that exist on the code behind page
本文介绍了如何显示代码隐藏页面上存在的已分配角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须在学校系统的网站上添加三个新角色,这样三个角色的用户才能看到正确的主页。下面是代码。我分配了最后三个角色(自助餐厅,建筑工程师和保安员):
I have to add three new roles to the school system's web site so users in these three roles will see the correct main page. Below is the code. I assigned the last three roles (Cafeteria, Building Engineer, and Security Guard):
public partial class WebForm1 : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var username = User.Identity.Name;
SqlConnection MyConnection = new SqlConnection("server=myserver\\sql2008;database=Schools;Trusted_Connection=True;");
SqlDataAdapter MyDataAdapter = new SqlDataAdapter("GetUserInfo", MyConnection);
MyDataAdapter.SelectCommand.CommandType = CommandType.StoredProcedure;
MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@userinfo", SqlDbType.VarChar, 40));
MyDataAdapter.SelectCommand.Parameters["@userinfo"].Value = (userinfo);
MyDataAdapter.SelectCommand.Parameters.Add(new SqlParameter("@userrole", SqlDbType.VarChar, 40));
MyDataAdapter.SelectCommand.Parameters["@userrole"].Direction = ParameterDirection.Output;
DataSet DS = new DataSet();
MyConnection.Open();
MyDataAdapter.Fill(DS, "UsersRole");
Session.Add("Role", DS);
string userrole = null;
userrole = MyDataAdapter.SelectCommand.Parameters[1].Value.ToString();
string role1 = "Principal";
string role2 = "AsstPrincipal";
string role3 = "SupportStaff";
string role4 = "FirstGrade";
string role5 = "SecondGrade";
string role6 = "ThirdGrade";
string role7 = "Cafeteria";
string role8 = "BuildingEngineers";
string role9 = "SecurityGuard";
if (userrole == role1)
{
Server.Transfer("PrincipalMP.aspx", true);
}
else if (userrole == role2)
{
Server.Transfer("AsstPrincipalMP.aspx", true);
}
else if (userrole == role3)
{
Server.Transfer("SupportStaffMP.aspx", true);
}
else if (userrole == role4)
{
Server.Transfer("FirstGradeMP.aspx", true);
}
else if (userrole == role5)
{
Server.Transfer("SecondGrade.aspx", true);
}
else if (userrole == role6)
{
Server.Transfer("ThirdGradeMP.aspx", true);
}
else if (userrole == role7)
{
Server.Transfer("CafeteriaMP.aspx", true);
}
else if (userrole == role8)
{
Server.Transfer("BuildingEngineersMP.aspx", true);
}
else if (userrole == role9)
{
Server.Transfer("SecurityGuardMP.aspx", true);
}
else
{
Server.Transfer("UnknownUser.aspx", true);
}
MyConnection.Close();
}
}
}
The problem I have is when I log in as a member of Cafeteria, BuildEngineer, or Security Guard, I am not taken to their assigned page - I am taken to the Unknown User main page.
I want to be sure the correct user role is actually being passed. How would I display that on the Unknown User Page?
}
我的尝试:
Google搜索,技术主板
What I have tried:
Google search, technical boards
推荐答案
请在visual studio中使用调试器。这是您找到代码无法正常工作的确切原因的唯一方法。
查看分配给'userrole'变量的值是什么声明。
Please use debugger in visual studio . This is the only way you can find out the exact reason why your code is not working as expected .
See what is the value assigned to 'userrole' variable in the below statement.
userrole = MyDataAdapter.SelectCommand.Parameters[1].Value.ToString();
查看以下链接进行调试。
使用调试器浏览代码 [ ^ ]
掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]
这篇关于如何显示代码隐藏页面上存在的已分配角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文