MasterPage.master
<asp:TreeView ID="tvUserRightMenu" runat="server" BackColor="#F8B109" ExpandImageUrl="~/images/menuopen.gif" ShowLines="True">
</asp:TreeView>
public partial class MasterPage : System.Web.UI.MasterPage
{
LoginManage lm = new LoginManage();
UserManage um = new UserManage();
MemberManage mm = new MemberManage();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string userId = Session["userid"].ToString();//获得用户的登陆id
DisplayUserInfo(userId);//初始化用户基本信息
DisplayUserMenu(userId);//显示该用户拥有权限的功能菜单
DataTable dataTablePostUntreadInfo = mm.GetPostFailedInfoDataTable(userId);
if (dataTablePostUntreadInfo.Rows.Count > 0)
{
imgNew.Visible = true;
string postHistoryId = dataTablePostUntreadInfo.Rows[0]["posthistoryid"].ToString();
imgNew.Attributes.Add("OnClick", "ScanPostDetail('1','" + postHistoryId + "');");
}
else
{
imgNew.Visible = false;
}
}
}
/// <summary>
/// 显示该用户基本信息
/// </summary>
/// <param name="userId">用户id</param>
protected void DisplayUserInfo(string userId)
{
DataTable dataTableUserInfo = um.GetUserInfoByUserId(userId);//根据用户id获得用户信息表
foreach (DataRow dr in dataTableUserInfo.Rows)
{
this.lblUserId.Text = dr["userid"].ToString();
this.lblUserName.Text = dr["username"].ToString();
this.lblUserMoney.Text = dr["money"].ToString();
this.lblUserType.Text = dr["rolename"].ToString();
}
}
/// <summary>
/// 创建第一层节点
/// </summary>
/// <param name="userId"></param>
public void DisplayUserMenu(string userId)
{
tvUserRightMenu.Nodes.Clear();//避免重复加载节点,先清除所有节点
DataTable dataTableUserFatherNode = lm.GetUserRightDataTable(userId,"0");//获得所有第一层节点
foreach (DataRow dr in dataTableUserFatherNode.Rows)
{
string menuId = dr["nodeid"].ToString();//第一层节点id
string menuName = dr["displayname"].ToString();//第一层节点显示名称
TreeNode fatherNode = this.CreatTreeNode(menuName, menuId, "", "Images/menuclose.gif");//根据节点信息,创建第一层节点
if (dataTableUserFatherNode.Rows.Count > 0)
{
SerCreateTree(userId,menuId, fatherNode);//创建子节点
}
tvUserRightMenu.Nodes.Add(fatherNode);//将第一层节点加入到用户权限TreeView中
}
}
//创建第二层结点
private void SerCreateTree(string userId,string menuId, TreeNode fatherNode)
{
DataTable dataTableUserChildNode = lm.GetUserRightDataTable(userId, menuId);//获得menuId节点的所有子节点
foreach (DataRow dr in dataTableUserChildNode.Rows)
{
string childMenuId = dr["nodeid"].ToString();//第二层节点id
string childMenuName = dr["displayname"].ToString();//第二层节点名称
string nodeURL = ResolveUrl(dr["NodeUrl"].ToString().Trim());//将路径转换为在客户端可用的URL
TreeNode childNode = this.CreatTreeNode(childMenuName, childMenuId, nodeURL, "Images/CloseTree.gif");//根据节点信息,创建第二层节点
AddTree(fatherNode, childNode);//将子节点加入到父节点中
}
}
/// <summary>
/// 创建一个树节点,返回一个树节点对象,参数内容是:
/// 节点名称,节点ID,链接地址,正常图标,展开后的图标
/// </summary>
private TreeNode CreatTreeNode(string strText, string strId, string strUrl, string strImg)
{
TreeNode newNode = new TreeNode();
newNode.Text = strText;
newNode.Value = strId;
newNode.NavigateUrl = strUrl;
newNode.ImageUrl = strImg;
return newNode;
}
/// <summary>
/// 把子节点添加到父节点当中
/// </summary>
private void AddTree(TreeNode FatherNode, TreeNode ChildNode)
{
FatherNode.ChildNodes.Add(ChildNode);
}
protected void btnLeave_ServerClick(object sender, EventArgs e)
{
string strLogin = ResolveUrl("ReLogin.aspx");//将路径转换为在客户端可用的URL
Response.Redirect(strLogin);
}
}
BLL
LoginManage.cs
GetUserRightDataTable()
/// <summary>
/// 自定义-获得用户权限信息表
/// </summary>
public DataTable GetUserRightDataTable(string userId, string ParentId)
{
DataTable userRightDataTable = sda.GetUserRightInfoDataTable(userId, ParentId);
if (userRightDataTable == null)
{
this.errMessage = "您还没有通过审核,请等待管理员审核后登陆!";
return userRightDataTable;
}
else
{
return userRightDataTable;
}
}
DAL
/// <summary>
/// 自定义-获得用户权限信息
/// </summary>
public DataTable GetUserRightInfoDataTable(string userId, string ParentId)
{
string sql = "select * from viewUserRight where userid='" + userId + "' and parentnodeid='" + ParentId + "'";
try
{
DataTable userRightDataTable = dbo.ExecQuery(sql);
return userRightDataTable;
}
catch
{
return null;
}
}