我想根据用户ID绑定菜单。
在我的登录页面上,我已经可以将用户ID传递到主页页面。
在首页页面上,使用用户ID 并显示“菜单”,该菜单可以授予特定用户的权限。
这是我的编码:
Login.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.Data.SqlClient;
using System.Data;
namespace OT_WorkFlow_Application
{
public partial class Login : System.Web.UI.Page
{
//string strqry, User, Password;
String User, Password;
String UserID;
String UserType;
int RowCount;
protected void Page_Load(object sender, EventArgs e)
{
lblErrorMessage.Visible = false;
}
protected void btnLogin_Click(object sender, EventArgs e)
{
using (SqlConnection sqlCon = new SqlConnection(@"Mysql connection;"))
{
using (SqlCommand cmd = new SqlCommand("sp_CheckUser", sqlCon))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, sqlCon))
{
DataTable dt = new DataTable();
da.Fill(dt);
RowCount = dt.Rows.Count;
for (int i = 0; i < RowCount; i++)
{
User = dt.Rows[i]["UserName"].ToString();
Password = dt.Rows[i]["Password"].ToString();
UserID = dt.Rows[i]["UserID"].ToString();
if (User == txtUserName.Text && Password == txtPassword.Text)
{
Session["UserName"] = User;
Session["UserID"] = UserID;
Response.Redirect("Home.aspx");
}
else
{
lblErrorMessage.Visible = true;
}
}
}
}
}
}
}
}
Home.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.IO;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespace OT_WorkFlow_Application
{
public partial class OT : System.Web.UI.MasterPage
{
SqlConnection sqlCon = new SqlConnection(@"Mysql connection;");
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData(0);
PopulateMenu(dt, 0, null);
}
}
private DataTable GetData(int UserID)
{
//Sql query for testing purpose
string query = "select m.* from tbpermission as per , [tbrolemodule] as rm, [tbrole] as r, [tbmodule] m, [tblUser] u where per.RoleID = rm.RoleID and rm.RoleID = r.RoleID and rm.moduleID = m.moduleID and per.Userid = u.Userid";
string LoginDBConnectionString1 = ConfigurationManager.ConnectionStrings["LoginDBConnectionString1"].ConnectionString;
using (SqlConnection con = new SqlConnection(LoginDBConnectionString1))
{
DataTable dt = new DataTable();
//using (SqlCommand cmd = new SqlCommand("Sp_Module", sqlCon))
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Parameters.AddWithValue("@UserID", UserID);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
private void PopulateMenu(DataTable dt, int UserID, MenuItem parentMenuItem)
{
string currentPage = Path.GetFileName(Request.Url.AbsolutePath);
foreach (DataRow row in dt.Rows)
{
MenuItem menuItem = new MenuItem
{
//Value = row["UserID"].ToString();
Value = row["ModuleID"].ToString(),
Text = row["Name"].ToString(),
//Text1 = row["Description"].ToString(),
NavigateUrl = row["Url"].ToString(),
Selected = row["Url"].ToString().EndsWith(currentPage, StringComparison.CurrentCultureIgnoreCase)
};
if (UserID == 0 )
{
Menu1.Items.Add(menuItem);
DataTable dtChild = this.GetData(int.Parse(menuItem.Value));
PopulateMenu(dtChild, int.Parse(menuItem.Value), menuItem);
}
else
{
parentMenuItem.ChildItems.Add(menuItem);
}
}
}
}
}
下面的图像是SQL代码: SQL Query From DB
我相信问题出在Home.aspx.cs中。
不确定如何修改父子编码
敬请指教。
答案 0 :(得分:0)
您似乎将UserID
存储在会话状态中。在另一页中,您可以从会话阶段读取该值并使用它:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
int UserID = 0;
if(Session["UserName"] != null) int.TryParse(Session["UserName"].ToString(), out UserID);
DataTable dt = this.GetData(UserID);
PopulateMenu(dt, UserID, null);
}
}
您还应该查找内置的asp.net授权和身份验证,因为它比实施您自己的更为完整和安全。
答案 1 :(得分:0)
您正在递归调用中加载菜单。我认为您正在尝试获取父菜单,然后为父菜单项加载所有子菜单项,并在用户无法访问它们的同时过滤它们。
当您递归调用函数时要在代码中传递userID的menuID时,需要将GetData(int userID)函数更新为GetData(int menuItemParentID,int userID)。