简体   繁体   English

带复选框的Asp.net Treeview

[英]Asp.net Treeview with check boxes

Below is my treeview control in aspx page 这是我在aspx页面中的treeview控件

 <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: 下面是我在浏览器中运行Web应用程序时的树视图图像:
在此处输入图片说明

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. 当我选择任何子级或任何根级时,将触发回发事件,并基于选择要绑定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. 我建议您将子节点值和父节点值一起附加,这样,当您要根据用户选择绑定gridview时,这对您来说很容易。

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 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. 您可以使用treeview的Checkednodes属性来评估treeview的选中节点的值。

Check out following link that shows how to do it. 请查看以下链接,该链接显示了操作方法。

http://nilthakkar.blogspot.in/2009/05/retrieve-selected-treenode-value-at.html 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 这里的一些零件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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