Drag and Drop a tree view contols from the toolbox on the .aspx page
LeafNodeStyle-NodeSpacing="10px" LeafNodeStyle-HorizontalPadding="25px"
ShowCheckBoxes="All" runat="server" >
Add javascripts functions to check and uncheck all the checkboxes from tree hirerichy
On the coding Page
1.Add the attribute on the Page_load Events
protected void Page_Load(object sender, EventArgs e)
{
BindData();
TreeView1.Attributes.Add("onclick", "OnCheckBoxCheckChanged(event)");
}
2.Set the Parent and child relationship between the nodes
protected void BindData()
{
DataSet ds=new DataSet();
SqlConnection con=new SqlConnection("server=.;database=ABC;uid=dd;password=dd;");
con.Open();
SqlDataAdapter adpmenu=new SqlDataAdapter("Select * from tbl_Menu",con);
SqlDataAdapter adpsubmenu=new SqlDataAdapter("Select * from tbl_SubMenu",con);
adpmenu.Fill(ds,"tbl_Menu");
adpsubmenu.Fill(ds,"tbl_SubMenu");
con.Close();
ds.Relations.Add("SuppToProd", ds.Tables["tbl_Menu"].Columns["Menu_Id"], ds.Tables["tbl_SubMenu"].Columns["Menu_Id"]);
TreeNode nodemenu ;
TreeNode nodesubmenu;
foreach(DataRow rowmenu in ds.Tables["tbl_Menu"].Rows)
{
nodemenu = new TreeNode();
nodemenu.Text = rowmenu["Menu_Name"].ToString() ;
nodemenu.Value = rowmenu["Menu_Id"].ToString() ;
TreeView1.Nodes.Add(nodemenu);
foreach( DataRow rowsubMenu in rowmenu.GetChildRows("SuppToProd"))
{
nodesubmenu = new TreeNode();
nodesubmenu.Text = rowsubMenu["Sub_MenuName"].ToString();
nodesubmenu.Value = rowsubMenu["Sub_MenuId"].ToString() ;
nodemenu.ChildNodes.Add(nodesubmenu);
}
}
}
3.Insert into the database from the TreeView Selection
protected void btnClick_Click(object sender, EventArgs e)
{
string subMenuId = "";
foreach (TreeNode var in TreeView1.Nodes )
{
foreach (TreeNode var1 in var.ChildNodes)
{
if (var1.Checked == true)
{
subMenuId = subMenuId +","+ var1.Value;
}
}
}
if (subMenuId != null && subMenuId != string.Empty)
subMenuId = subMenuId.Substring(1, subMenuId.Length - 1);
adminobj.Usp_Admin_MenuPermission(Convert.ToInt32(this.User.Identity.Name), subMenuId);
}
ShowCheckBoxes="All" runat="server" >
Add javascripts functions to check and uncheck all the checkboxes from tree hirerichy
On the coding Page
1.Add the attribute on the Page_load Events
protected void Page_Load(object sender, EventArgs e)
{
BindData();
TreeView1.Attributes.Add("onclick", "OnCheckBoxCheckChanged(event)");
}
2.Set the Parent and child relationship between the nodes
protected void BindData()
{
DataSet ds=new DataSet();
SqlConnection con=new SqlConnection("server=.;database=ABC;uid=dd;password=dd;");
con.Open();
SqlDataAdapter adpmenu=new SqlDataAdapter("Select * from tbl_Menu",con);
SqlDataAdapter adpsubmenu=new SqlDataAdapter("Select * from tbl_SubMenu",con);
adpmenu.Fill(ds,"tbl_Menu");
adpsubmenu.Fill(ds,"tbl_SubMenu");
con.Close();
ds.Relations.Add("SuppToProd", ds.Tables["tbl_Menu"].Columns["Menu_Id"], ds.Tables["tbl_SubMenu"].Columns["Menu_Id"]);
TreeNode nodemenu ;
TreeNode nodesubmenu;
foreach(DataRow rowmenu in ds.Tables["tbl_Menu"].Rows)
{
nodemenu = new TreeNode();
nodemenu.Text = rowmenu["Menu_Name"].ToString() ;
nodemenu.Value = rowmenu["Menu_Id"].ToString() ;
TreeView1.Nodes.Add(nodemenu);
foreach( DataRow rowsubMenu in rowmenu.GetChildRows("SuppToProd"))
{
nodesubmenu = new TreeNode();
nodesubmenu.Text = rowsubMenu["Sub_MenuName"].ToString();
nodesubmenu.Value = rowsubMenu["Sub_MenuId"].ToString() ;
nodemenu.ChildNodes.Add(nodesubmenu);
}
}
}
3.Insert into the database from the TreeView Selection
protected void btnClick_Click(object sender, EventArgs e)
{
string subMenuId = "";
foreach (TreeNode var in TreeView1.Nodes )
{
foreach (TreeNode var1 in var.ChildNodes)
{
if (var1.Checked == true)
{
subMenuId = subMenuId +","+ var1.Value;
}
}
}
if (subMenuId != null && subMenuId != string.Empty)
subMenuId = subMenuId.Substring(1, subMenuId.Length - 1);
adminobj.Usp_Admin_MenuPermission(Convert.ToInt32(this.User.Identity.Name), subMenuId);
}
Comments