Click here to Skip to main content
15,069,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
*************************Menu************************* 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 System.Data.SqlClient; using System.Configuration; namespace DynamicMenu { public partial class Menu : System.Web.UI.Page { private readonly Master objMaster = new Master(); private DataView dv; private MenuItem menuNode; private string connStrg = ConfigurationManager.ConnectionStrings["connString"].ToString(); protected void Page_Load(object sender, EventArgs e) { LoadMenu(); } #region "Menu" //load the menu according to user rights private void LoadMenu() { mnuDynmic.Items.Clear(); new MenuItem(); dv = GetMenu(); dv.RowFilter = "parent_id=0"; for (objMaster.I = 0; objMaster.I <= dv.Count - 1; objMaster.I++) { menuNode = new MenuItem { Text = dv[objMaster.I].Row["menu_name"].ToString(), Value = dv[objMaster.I].Row["menu_id"].ToString(), NavigateUrl = dv[objMaster.I].Row["url"].ToString() }; menuNode.Selectable = menuNode.NavigateUrl != ""; mnuDynmic.Items.Add(menuNode); Addnode(menuNode, Convert.ToInt32(menuNode.Value)); dv.RowFilter = "parent_id=0"; } } private DataView GetMenu() { DataTable dt = new DataTable(); DataView dv = new DataView(); SqlConnection conn = new SqlConnection(connStrg); conn.Open(); SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM vw_usermenu", conn); da.Fill(dt); dv=dt.AsDataView(); return dv; } private void Addnode(MenuItem pnode, int pid) { int j; dv.RowFilter = "parent_id=" + pid; for (j = 0; j <= dv.Count - 1; j++) { var node = new MenuItem { Text = dv[j].Row["menu_name"].ToString(), Value = dv[j].Row["menu_id"].ToString(), NavigateUrl = dv[j].Row["url"].ToString() }; node.Selectable = node.NavigateUrl != ""; pnode.ChildItems.Add(node); Addnode(node, Convert.ToInt32(node.Value)); dv.RowFilter = "parent_id=" + pid; } } #endregion } public class Master { public int I; public int J; public string Url; public string Page; public int ProjectId; } } *********************User Creation********************************* 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 System.Data.SqlClient; using System.Configuration; using System.Collections; namespace DynamicMenu { public partial class UserCreation : System.Web.UI.Page { private DataView dvRights = new DataView(); private string connStrg = ConfigurationManager.ConnectionStrings["connString"].ToString(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { LoadTreeview(); SetTreeViewPriv(); } } #region Functions #region Treeview private void LoadTreeview() { TreeNode objTreenode; tvRights.Nodes.Clear(); dvRights = GetAllMenu(); dvRights.RowFilter = "parent_id=0"; for (int i = 0; i <= dvRights.Count - 1; i++) { objTreenode = new TreeNode(); objTreenode.Text = dvRights[i].Row["menu_name"].ToString(); objTreenode.Value = dvRights[i].Row["menu_id"].ToString(); objTreenode.ShowCheckBox = true; tvRights.Nodes.Add(objTreenode); AddNode(objTreenode, Convert.ToInt32(objTreenode.Value)); dvRights.RowFilter = "parent_id=0"; } //tvRights.CollapseAll(); } private DataView GetAllMenu() { DataTable dtRght = new DataTable(); DataView dvRght = new DataView(); SqlConnection conn = new SqlConnection(connStrg); conn.Open(); SqlDataAdapter da = new SqlDataAdapter("SELECT menu_id,menu_name,parent_id FROM tfx_menu", conn); da.Fill(dtRght); dvRght = dtRght.AsDataView(); return dvRght; } private void AddNode(TreeNode pNode, int pId) { TreeNode objNode; dvRights.RowFilter = "parent_id=" + pId; for (int k = 0; k <= dvRights.Count - 1; k++) { objNode = new TreeNode(); objNode.Text = dvRights[k].Row["menu_name"].ToString(); objNode.Value = dvRights[k].Row["menu_id"].ToString(); objNode.ShowCheckBox = true; pNode.ChildNodes.Add(objNode); AddNode(objNode, Convert.ToInt32(objNode.Value)); dvRights.RowFilter = "parent_id=" + pId; } } private void SetTreeViewPriv() { dvRights = GetMenuPrivilege(); dvRights.RowFilter = "parent_id=0"; if (dvRights.Count != 0) { for (int k = 0; k < tvRights.Nodes.Count; k++) { for (int i = 0; i <= dvRights.Count - 1; i++) { if (Convert.ToInt32(tvRights.Nodes[k].Value) == Convert.ToInt32(dvRights[i].Row["menu_id"].ToString())) { tvRights.Nodes[k].Checked = true; setChild(k, Convert.ToInt32(dvRights[i].Row["menu_id"].ToString()), tvRights.Nodes[k]); break; } } dvRights.RowFilter = "parent_id=0"; } } } private DataView GetMenuPrivilege() { DataTable dtPrvlg = new DataTable(); DataView dvPrvlg = new DataView(); SqlConnection conn = new SqlConnection(connStrg); conn.Open(); SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM vw_usermenu", conn); da.Fill(dtPrvlg); dvPrvlg = dtPrvlg.AsDataView(); return dvPrvlg; } private void setChild(int pnode, int pid, TreeNode treeNode) { dvRights.RowFilter = "parent_id=" + pid; if (dvRights.Count != 0) { for (int k = 0; k < treeNode.ChildNodes.Count; k++) { for (int j = 0; j <= dvRights.Count - 1; j++) { if (Convert.ToInt32(treeNode.ChildNodes[k].Value) == Convert.ToInt32(dvRights[j].Row["menu_id"].ToString())) { treeNode.ChildNodes[k].Checked = true; setChild(k, Convert.ToInt32(dvRights[j].Row["menu_id"].ToString()), treeNode.ChildNodes[k]); break; } } dvRights.RowFilter = "parent_id=" + pid; } } } #endregion private string GetCheckedMenus() { int status = 0; string getCheck = ""; for (int i = 0; i <= tvRights.Nodes.Count - 1; i++) { if (tvRights.Nodes[i].Checked == true) { getCheck = getCheck + "," + tvRights.Nodes[i].Value; if (tvRights.Nodes[i].ChildNodes.Count != 0) { for (int j = 0; j <= tvRights.Nodes[i].ChildNodes.Count - 1; j++) { if (tvRights.Nodes[i].ChildNodes[j].Checked == true) { status = 1; getCheck = getCheck + "," + tvRights.Nodes[i].ChildNodes[j].Value; if (tvRights.Nodes[i].ChildNodes[j].ChildNodes.Count != 0) { for (int x = 0; x <= tvRights.Nodes[i].ChildNodes[j].ChildNodes.Count - 1; x++) { if (tvRights.Nodes[i].ChildNodes[j].ChildNodes[x].Checked == true) { getCheck = getCheck + "," + tvRights.Nodes[i].ChildNodes[j].ChildNodes[x].Value; } } } } } } } } if (status == 0) getCheck = ""; if (getCheck != "") getCheck = getCheck.Substring(1); return getCheck; } #endregion protected void btnSave_Click(object sender, EventArgs e) { GetCheckedMenus(); } } //Class Declarations public class UserRights { public int user_right_id; public int user_id; public int menu_id; } public class User { public int user_id; public string username; public string menuIds; public ArrayList UserRights = new ArrayList(); } }
Posted
Comments
CHill60 29-Jun-14 16:23pm
   
Format your code and state what your problem is

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900