Tuesday 27 June 2017

Dynamic Treeview in asp.net using Entity Framework




<asp:TreeViewID="TreeView2"runat="server">
<HoverNodeStyleFont-Underline="True"ForeColor="#5555DD"/>
<NodeStyleFont-Names="Verdana"Font-Size="8pt"ForeColor="Black"HorizontalPadding="5px"NodeSpacing="0px"VerticalPadding="0px"/>
<ParentNodeStyleFont-Bold="False"/>
<SelectedNodeStyleFont-Underline="True"ForeColor="#5555DD"HorizontalPadding="0px"VerticalPadding="0px"/>
</asp:TreeView>




Database1Entities db = new Database1Entities();
Tbl_option qt = new Tbl_option();
Tbl_Question q = newTbl_Question();

Protectedvoid Page_Load(object sender, EventArgs e)
        {
          if(!this.IsPostBack)
            {
              BindTreeViewControl();
            }
        }
//for parent node
  #region asp
 Privatevoid BindTreeViewControl()
        {
         Var qtbl = (from a indb.Tbl_Questionselect a).ToList();
         for (inti = 0; i<qtbl.Count; i++)
            {
      TreeNode root = newTreeNode(qtbl[i].Question.ToString(), qtbl[i].QansId.ToString());
     //   root.SelectAction = TreeNodeSelectAction.Expand;
     TreeView2.Nodes.Add(root);
     CreateNode(root);
            }
        }
//For creating childnode
Publicvoid CreateNode(TreeNode node)
        {
       Int QId = Convert.ToInt32(node.Value);
       Var qtbl = (from a indb.Tbl_option where a.QansId == Qid select a).ToList();
       for (int i = 0; i<qtbl.Count; i++)
     TreeNode Childnode = new TreeNode(qtbl[i].ans.ToString(), qtbl[i].ansId.ToString());
     Childnode.SelectAction = TreeNodeSelectAction.Expand;
     Childnode.ShowCheckBox = true;
     node.ChildNodes.Add(Childnode);
//    CreateNode(Childnode, Dt);
            }
        }
 #endregion

No comments:

Post a Comment