简体   繁体   中英

Asp.net Treeview with check boxes

Below is my treeview control in aspx page

 <asp:TreeView ID="TvCategories" runat="server" ShowCheckBoxes="All"    OnSelectedNodeChanged="TvCategories_SelectedNodeChanged">
</asp:TreeView>

I am binding it dynamically as below:

    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    ds = BindCategories();
    dt = ds.Tables[0];

   for (int i = 0; i < dt.Rows.Count; i++)
   {
      TreeNode firstchild = new TreeNode();
      firstchild.Text = " Shoes";
      firstchild.SelectAction = TreeNodeSelectAction.Select;
      TvCategories.Nodes.Add(firstchild);
      DataSet ds1 = new DataSet();
      ds1 = BindSubCategories(dt.Rows[i]["InventoryType"].ToString());
      DataTable dt1 = ds1.Tables[0];

      for (int j = 0; j < dt1.Rows.Count; j++)
      {
           TreeNode childnode = new TreeNode();
           childnode.SelectAction = TreeNodeSelectAction.Select;

           if (j == 0)
              childnode.Text = "Nike";
           if (j == 1)
              childnode.Text = "Rebok";
           if (j == 2)
              childnode.Text = "Addidas";
              firstchild.ChildNodes.Add(childnode);

       }
  } 

Below is the image of treeview when I run the Web application in browser:
在此处输入图片说明

What I want to do?

By selecting the root node will also select all the child node and visa versa
when I select any child or any root, an postback event will fire and based on selecting I want to bind gridview.

I would suggest you to append your child node value along with parent node value so when you're going to bind gridview based on user selection, it would be easy for you.

Check out below link that shows how to check parent-child node:

http://nilthakkar.blogspot.in/2009/04/check-uncheck-treeview-checkboxes-with_13.html

You can have valued of checked nodes of treeview with Checkednodes property of treeview.

Check out following link that shows how to do it.

http://nilthakkar.blogspot.in/2009/05/retrieve-selected-treenode-value-at.html

Use this code to post back on check changed:

<script language="javascript" type="text/javascript">
   e = e || window.event;
   var o = e.srcElement || e.target;
</script> 
    <asp:TreeView ID="TvCategories" runat="server" ShowCheckBoxes="All onclick="postBackByObject(this);" >
    </asp:TreeView>
<script language="javascript" type="text/javascript">
    document.getElementById('<%=TvCategories.ClientID %>').addEventListener('click', postBackByObject);

</script> 

some parts from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM