Asp.net MVC会员管理系统
功能比较简单
源代码在线查看: function.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;
using SkyiSite.Models;
using SkyiSite.DB;
using SkyiSite.Tooltip;
using System.IO;
namespace SkyiSite.Views.Account
{
public partial class iFunction : System.Web.Mvc.ViewPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
BindUser();
BindSite();
}
}
SkyiSite.DB.AdoHelper ado = SkyiSite.DB.AdoHelper.Instance;
string sql = "";
private void BindGrid()
{
sql = "Select RoleId,RoleName from aspnet_Roles";
DataTable table = ado.ExecuteDataset(sql).Tables[0];
CustomersGridView.DataSource = table;
CustomersGridView.DataBind();
}
private void BindSite()
{
TreeNode rootNode = new TreeNode( Server.MapPath("~/"));
TreeView2.Nodes.Add(rootNode);
LoadChildNode(rootNode);
}
private void LoadChildNode(TreeNode node)
{
DirectoryInfo directory = new DirectoryInfo(node.Value);
foreach (DirectoryInfo sub in directory.GetDirectories())
{
TreeNode subNode = new TreeNode(sub.Name);
subNode.Value = sub.FullName;
try
{
if (sub.GetDirectories().Length > 0 || sub.GetFiles().Length > 0)
{
subNode.SelectAction = TreeNodeSelectAction.Select;
subNode.NavigateUrl = "#";
node.ChildNodes.Add(subNode);
LoadChildNode(subNode);
}
}
catch
{
node.ChildNodes.Add(subNode);
}
}
foreach (FileInfo fi in directory.GetFiles())
{
TreeNode ssubNode = new TreeNode(fi.Name);
ssubNode.NavigateUrl = "#";
ssubNode.SelectAction = TreeNodeSelectAction.Select;
node.ChildNodes.Add(ssubNode);
}
}
private void BindUser()
{
sql = "Select UserId,UserName from aspnet_Users";
DataTable table = ado.ExecuteDataset(sql).Tables[0];
UserGrid.DataSource = table;
UserGrid.DataBind();
}
private void BindUserGrid(string RoleID)
{
ResetCheckBox();
if (RoleID == "") return;
string sql = "Select UserId from aspnet_UsersInRoles where RoleId='" + RoleID + "'";
DataTable table = ado.ExecuteDataset(sql).Tables[0];
for (int i = 0; i < UserGrid.DataKeys.Count; i++)
{
if (table.Select("UserID='" + UserGrid.DataKeys[i].Value.ToString() + "'").Length > 0)
{
((CheckBox)UserGrid.Rows[i].Cells[1].FindControl("btnGrid_CheckBox_forUser")).Checked = true;
}
}
if (table != null) table.Dispose();
}
private void ResetCheckBox()
{
foreach (GridViewRow row in UserGrid.Rows)
{
((CheckBox)row.Cells[1].FindControl("btnGrid_CheckBox_forUser")).Checked = false;
}
}
#region ==========创建一个空的权限树==========
private void BindTree()
{
////-----------建立权限树,没有任何选中的权限----------------////
sql = "Select [SY_Function].[funID],[funName],parentID from [SY_Function] order by [parentID],[sequence]";
DataTable table = ado.ExecuteDataset(sql).Tables[0];
TreeNode rootNode = new TreeNode();
rootNode.Text = "功能分类";
rootNode.Value = "0";
this.TreeView1.Nodes.Add(rootNode);
AddNote(table, "0", rootNode);
}
public void AddNote(DataTable table, string ParentID, TreeNode parentNode)
{
foreach (DataRow row in table.Select("ParentID=" + ParentID))
{
TreeNode node = new TreeNode();
node.Text = row["funName"].ToString();
node.Value = row["funID"].ToString();
parentNode.ChildNodes.Add(node);
AddNote(table, row["funID"].ToString(), node);
}
}
#endregion
#region ===========为角色绑定权限===========
private void BindTree_Role(String RoleID)
{
////-------------为权限树添加权限--------------////
sql = "Select [funID] from [SY_FunctionInRole] where RoleID='" + RoleID + "'";
DataTable table = ado.ExecuteDataset(sql).Tables[0];
foreach (TreeNode node in TreeView1.Nodes)
{
if (table.Select("FunID=" + node.Value).Length > 0)
node.Checked = true;
if (node.ChildNodes.Count > 0)
BindChildTree(table, node);
}
}
private void BindChildTree(DataTable table, TreeNode ParentNode)
{
foreach (TreeNode node in ParentNode.ChildNodes)
{
if (table.Select("FunID=" + node.Value).Length > 0)
node.Checked = true;
if (node.ChildNodes.Count > 0)
BindChildTree(table, node);
}
}
#endregion
#region ===========为用户绑定权限===========
private void BindTree_User(String UserID)
{
////-------------为权限树添加权限--------------////
sql = "Select [funID],[IsOwn] from [SY_FunctionInUser] where UserID='" + UserID + "'";
DataTable table = ado.ExecuteDataset(sql).Tables[0];
foreach (TreeNode node in TreeView1.Nodes)
{
if (table.Select("IsOwn=false and FunID=" + node.Value).Length > 0)
{
node.Checked = false;
}
else if (table.Select("IsOwn=true and FunID=" + node.Value).Length > 0)
{
node.Checked = true;
}
if (node.ChildNodes.Count > 0)
BindUserChildTree(table, node);
}
}
private void BindUserChildTree(DataTable table, TreeNode ParentNode)
{
foreach (TreeNode node in ParentNode.ChildNodes)
{
if (table.Select("IsOwn=false and FunID=" + node.Value).Length > 0)
{
node.Checked = false;
}
else if (table.Select("IsOwn=true and FunID=" + node.Value).Length > 0)
{
node.Checked = true;
}
if (node.ChildNodes.Count > 0)
BindUserChildTree(table, node);
}
}
#endregion
#region =========GridView操作==========
protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow pagerRow = this.CustomersGridView.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
CustomersGridView.PageIndex = pageList.SelectedIndex;
}
protected void PageDropDownList_SelectedIndexChanged1(Object sender, EventArgs e)
{
GridViewRow pagerRow = this.UserGrid.BottomPagerRow;
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
UserGrid.PageIndex = pageList.SelectedIndex;
}
protected void CustomersGridView_DataBound(Object sender, EventArgs e)
{
WebGridView.WebGridView Grid = sender as WebGridView.WebGridView;
GridViewRow pagerRow = Grid.BottomPagerRow;
LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");
if (Grid.PageIndex == 0)
{
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
}
else if (Grid.PageIndex == Grid.PageCount - 1)
{
linkBtnLast.Enabled = false;
linkBtnNext.Enabled = false;
}
else if (Grid.PageCount {
linkBtnFirst.Enabled = false;
linkBtnPrev.Enabled = false;
linkBtnNext.Enabled = false;
linkBtnLast.Enabled = false;
}
DropDownList pageList = (DropDownList)pagerRow.Cells[0].FindControl("PageDropDownList");
Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");
if (pageList != null)
{
for (int i = 0; i < Grid.PageCount; i++)
{
int pageNumber = i + 1;
ListItem item = new ListItem(pageNumber.ToString() + "/" + Grid.PageCount.ToString(), pageNumber.ToString());
if (i == Grid.PageIndex)
{
item.Selected = true;
}
pageList.Items.Add(item);
}
}
if (pageLabel != null)
{
int currentPage = Grid.PageIndex + 1;
pageLabel.Text = "当前页: " + currentPage.ToString() +
" / " + Grid.PageCount.ToString();
}
}
#endregion
protected void CustomersGridView_SelectedIndexChanged(object sender, EventArgs e)
{
ViewState["RoleID"] = null;
ViewState["UserID"] = null;
String RoleID = CustomersGridView.SelectedDataKey.Value.ToString();
ViewState["RoleID"] = RoleID;
BindUserGrid(RoleID);
BindTree_Role(RoleID);
}
protected void UserGrid_SelectedIndexChanged(object sender, EventArgs e)
{
ViewState["UserID"] = null;
String UserID = this.UserGrid.SelectedDataKey.Value.ToString();
if (UserID == null || UserID == "")
{
MessageBox.Show("没有选中任何行!");
return;
}
ViewState["UserID"] = UserID;
BindTree_User(UserID);
}
string ids = string.Empty;
private void ViewAllTreeNode()
{
ids = "";
foreach (TreeNode node in TreeView1.Nodes)
{
if (node.Checked) ids += node.Value + ",";
if (node.ChildNodes.Count > 0) ViewAllTreeNode_Child(node);
}
if (ids != "") ids = ids.Remove(ids.Length - 1);
}
private void ViewAllTreeNode_Child(TreeNode ParentNode)
{
ids = "";
foreach (TreeNode node in ParentNode.ChildNodes)
{
if (node.Checked) ids += node.Value + ",";
if (node.ChildNodes.Count > 0) ViewAllTreeNode_Child(node);
}
}
}
}