using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.UI.MobileControls;
using Entity;
using System.Collections.Generic;
using System.Text;
namespace UI
{
public partial class WscgroupMaintain : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["GroupName"] != null && Session["Description"]!=null)
{
TextBoxGroupName.Text = Session["GroupName"].ToString();
TextBoxDescription.Text = Session["Description"].ToString();
Session["GroupName"] = "";
Session["Description"] = "";
}
this.binddata();
List<Menusort> list = new List<Menusort>();
//定义一个根节点
Menusort root = new Menusort("root", "1");
list.Add(root);
//调用排序方法
list = Gettree(root, list, 0);
//移除根节点
list.Remove(root);
//绑定数据源
GridViewMenu.DataSource = list;
GridViewMenu.DataBind();
//如果是编辑,则权限复选按钮默认选中
if(Session["add"].ToString()=="edit")
{
TextBoxGroupName.ReadOnly = true;
GroupMenuEntity gm = new GroupMenuEntity();
gm.GroupName = TextBoxGroupName.Text.Trim();
DataSet ds = gm.SelectGroupMenuByGroupName();
DataTable dt = ds.Tables[0];
foreach (DataRow dr in dt.Rows)
{
for (int i = 0; i < GridViewMenu.Rows.Count; i++)
{
if (dr["function_name"].ToString().Trim() == GridViewMenu.Rows[i].Cells[1].Text.Trim())
{
((CheckBox)GridViewMenu.Rows[i].Cells[0].FindControl("CheckBoxItems")).Checked = true;
break;
}
}
}
}
}
}
/// <summary>
/// 对表中的数据重新排序
/// </summary>
/// <param name="menu"></param>
/// <param name="list"></param>
/// <param name="level"></param>
/// <returns>list</returns>
private List<Menusort> Gettree(Entity.Menusort menu, List<Menusort> list, int level)
{
foreach (Menusort m in Getchild (menu))
{
m.Name = Rename(m.Name, level);
list.Insert(list.IndexOf(menu)+1,m);
if (Getchild(m).Count > 0)//如果有子节点则继续查找
list = Gettree(m,list,level+1);
}
return list;
}
/// <summary>
/// 重命名子节点
/// </summary>
/// <param name="name"></param>
/// <param name="level"></param>
/// <returns></returns>
private string Rename(string name, int level)
{
string space = "";
if (level != 0)
space = " ";
for (int i = 0; i < level; i++)
{
space = " "+ space ;
}
return space + name;
}
/// <summary>
/// 查询子节点
/// </summary>
/// <param name="menu"></param>
/// <returns>子节点数组</returns>
private List<Menusort> Getchild(Entity.Menusort menu)
{
DataSet ds = (DataSet)Session["Menu"];
DataTable dt = ds.Tables[0];
int count = dt.Rows.Count;
List<Menusort> list = new List<Menusort>();
for (int k = 0; k < count; k++)
{
DataRow dr2 = dt.Rows[k];
if (menu.ID == dr2["fatherid"].ToString())
{
Entity.Menusort menusort = new Menusort();
menusort.Name = dr2["function_name"].ToString();
menusort.ID = dr2["id"].ToString();
menusort.FatherID = dr2["fatherid"].ToString();
menusort.Description = dr2["description"].ToString();
list.Add(menusort);
}
}
return list;
}
private void binddata()
{
MenuEntity user = new MenuEntity();
Session["Menu"] = user.Select();
}
/// <summary>
/// Head里的CheckBox
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CheckBoxAll_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i < this.GridViewMenu.Rows.Count; i++)
{
((CheckBox)GridViewMenu.Rows[i].FindControl("CheckBoxItems")).Checked = ((CheckBox)this.GridViewMenu.HeaderRow.FindControl("CheckBoxAll")).Checked;
}
}
/// <summary>
/// 选中GridViewMenu中的一个CheckBox时,同时选中它的父节点和所有子节点,取消时同时取消它的所有子节点
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void CheckBoxItems_CheckedChanged(object sender, EventArgs e)
{
int index = ((GridViewRow)((CheckBox)sender).Parent.Parent).RowIndex;
string strid = GridViewMenu.DataKeys[index].Values["ID"].ToString();
string strfatherid = GridViewMenu.DataKeys[index].Values["FatherID"].ToString();
if (((CheckBox)GridViewMenu.Rows[index].FindControl("CheckBoxItems")).Checked)
{
this.GetChildTree(strid, index, true);
this.GetFatherTree(strfatherid, index, true);
}
else
{
this.GetChildTree(strid, index, false);
}
}
/// <summary>
/// 选中父节点
/// </summary>
/// <param name="id"></param>
/// <param name="index"></param>
/// <param name="bol"></param>
private void GetFatherTree(string id, int index, bool bol)
{
foreach (Menusort m in Getcheckboxfather(id, index))
{
int rowindex = Convert.ToInt32(m.Rowindex);
((CheckBox)GridViewMenu.Rows[rowindex].FindControl("CheckBoxItems")).Checked = bol;
if (Getcheckboxfather(m.FatherID, rowindex).Count > 0)//如果有子节点,则继续查找
GetFatherTree(m.FatherID, rowindex, bol);
}
}
/// <summary>
/// 选中子节点
/// </summary>
/// <param name="id"></param>
/// <param name="index"></param>
/// <param name="bol"></param>
private void GetChildTree(string id,int index,bool bol)
{
foreach (Menusort m in Getcheckboxchild(id,index))
{
int rowindex =Convert.ToInt32(m.Rowindex);
((CheckBox)GridViewMenu.Rows[rowindex].FindControl("CheckBoxItems")).Checked = bol;
if (Getcheckboxchild(m.ID,rowindex).Count > 0)
GetChildTree(m.ID, rowindex, bol);
}
}
/// <summary>
/// 查询父节点
/// </summary>
/// <param name="fatherid"></param>
/// <param name="index"></param>
/// <returns></returns>
private List<Menusort> Getcheckboxfather(string fatherid, int index)
{
List<Menusort> list = new List<Menusort>();
for (int i = index - 1; i>=0; i--)
{
string strid = GridViewMenu.DataKeys[i].Values["ID"].ToString();
if (fatherid == strid)
{
Entity.Menusort father = new Menusort();
father.Rowindex = i.ToString();
father.FatherID = GridViewMenu.DataKeys[i].Values["FatherID"].ToString();
list.Add(father);
break;
}
}
return list;
}
/// <summary>
/// 查询子节点
/// </summary>
/// <param name="id"></param>
/// <param name="index"></param>
/// <returns></returns>
private List<Menusort> Getcheckboxchild(string id,int index)
{
List<Menusort> list = new List<Menusort>();
for (int i = index+1; i < this.GridViewMenu.Rows.Count; i++)
{
string strfatherid = GridViewMenu.DataKeys[i].Values["FatherID"].ToString();
if (id == strfatherid)
{
Entity.Menusort child = new Menusort();
child.Rowindex = i.ToString();
child.ID = GridViewMenu.DataKeys[i].Values["ID"].ToString();
list.Add(child);
}
}
return list;
}
/// <summary>
/// 给组赋予权限
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ButtonSave_Click(object sender, EventArgs e)
{
string groupname = TextBoxGroupName.Text.Trim();
string description = TextBoxDescription.Text.Trim();
if (Session["add"].ToString() == "add")//给新组增加权限
{
GroupEntity ge = new GroupEntity();
ge.GroupName = groupname;
DataSet ds = ge.SelectGroup();
if (ds != null && ds.Tables[0].Rows.Count > 0)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(WscgroupMaintain), "AlertGroupName", "<script>alert('Soory,组名不能重名 !')</script>");
}
else
{
ge.Description = description;
ge.InsertGroup();
this.Checked();
}
}
else
{
//更新组权限
GroupMenuEntity gm = new GroupMenuEntity();
gm.GroupName = groupname;
gm.DeleteGrouplMenuByGroupName();//先删除
this.Checked();
}
}
/// <summary>
/// 根据选中的CheckBox进行更新
/// </summary>
private void Checked()
{
string groupname = TextBoxGroupName.Text.Trim();
string description = TextBoxDescription.Text.Trim();
List<String> functionnames = new List<String>();
for (int i = 0; i < this.GridViewMenu.Rows.Count; i++)
{
if (((CheckBox)GridViewMenu.Rows[i].FindControl("CheckBoxItems")).Checked)
{
string functionname = GridViewMenu.Rows[i].Cells[1].Text.Trim();
functionnames.Add(functionname);
}
}
//如果没有选择权限则返回
if (functionnames.Count == 0)
{
return;
}
else
{
GroupMenuEntity gm = new GroupMenuEntity();
gm.GroupName = groupname;
gm.InsertGroupMenus(functionnames);
}
Response.Redirect("WSCGroupQuery.aspx");
}
}
}
///TreeView
private static IList<SystemMenuCustomInfo> list;
private static IList<SystemMenuCustomInfo> listChildNode = ManagerController.GetISystemMenuCustomManager().GetAll();
private static void AppendChild(TreeNode tn, string ParentMenuCode)
{
listChildNode.Clear();
foreach (SystemMenuCustomInfo info in list)
{
if (info.ParentMenuCode == ParentMenuCode)
{
listChildNode.Add(info);
}
}
if (listChildNode.Count == 0)
{
tn.SelectedImageIndex = 0;
tn.StateImageIndex = 0;
tn.ImageIndex = 0;
}
for (int i = 0; i < listChildNode.Count; i++)
{
SystemMenuCustomInfo info = listChildNode[i];
TreeNode node = new TreeNode(info.MenuName);
node.SelectedImageIndex = 1;
node.StateImageIndex = 1;
node.ImageIndex = 1;
tn.Nodes.Add(node);
//递归调用
AppendChild(node, info.MenuCode);
listChildNode.Clear();
foreach (SystemMenuCustomInfo infon in list)
{
if (infon.ParentMenuCode == ParentMenuCode)
{
listChildNode.Add(infon);
}
}
}
}
public static void InitMenuTree(TreeView TreeMenu)
{
if (TreeMenu.Nodes.Count > 0)
TreeMenu.Nodes.Clear();
list = ManagerController.GetISystemMenuCustomManager().GetMenuCustomByUserCode(LoginData.Instance.UserCode);
TreeNode root = new TreeNode();
root.Text = "系统菜单";
root.SelectedImageIndex = 1;
root.StateImageIndex = 1;
root.ImageIndex = 1;
//BuildTree(TreeMenu.Nodes, "");
AppendChild(root, "");
TreeMenu.Nodes.Add(root); //将根节点加入到TreeView中去
}