asp.net treeview checkbox选择

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>treeView操作</title>
    <script type="text/javascript">
    //---------------------选择
     function ShowLearning()
    {
        var k=window.showModalDialog('Choosetree.aspx',window, 'dialogHeight: 550px; dialogWidth: 550px; edge: Raised; center: Yes; help: No; resizable: No; status: No;')
          if(k != null)
         {
           var str=k.split('-');
           document.getElementById("<%=Text1.ClientID%>").value=str[0];
           document.getElementById("<%=Text2.ClientID%>").value=str[1];
          }
    } 
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Text1" type="text"  runat="server" style="width: 359px"/><br />
        <br />
        <input id="Text2" type="text" runat="server" style="width: 355px" />
        &nbsp;
        <input id="Button1" style="width: 45px" type="button" value="选择" onclick="ShowLearning()" /></div>
    </form>
</body>
</html>

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Choosetree.aspx.cs" Inherits="Choosetree" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <script type="text/javascript" src="js/jquery-1.4.2.js"></script>
    <script type="text/javascript">
        $(document).ready(Bind);
        function Bind() {
             BindTreeView("TreeView1",true,false);//--------------------选中节点的时候是否也选中子节点
            // BindTreeView("TreeView1",false,true);//-----------第三个参数:选中节点的时候,所有的父节点是否选中。
        }
        ////第一个参数代表TreeView控件的ID,第二个参数:选中节点的时候是否也选中子节点,第三个参数:选中节点的时候,所有的父节点是否选中。           
        function BindTreeView(treeViewId,selectChild,selectParent) {

            $("#" + treeViewId + " input").click(function() {
                if (selectChild) {
                    var divItem = $(this).parent().parent().parent().parent().next();
                    divItem.find("input").attr("checked", $(this).get(0).checked);
                 
                 
                }
                if (selectParent) {
                    SelectParentNode(treeViewId, $(this));
                }
            });
        }
        function SelectParentNode(treeViewId,node) {
            if (node.get(0).checked == true) {
                var pdiv = node.parent().parent().parent().parent().parent();
                if (pdiv.get(0).id != treeViewId) {
                    pdiv.prev().find("input").attr("checked", node.get(0).checked);
                    SelectParentNode(treeViewId, pdiv.prev().find("input"));
                }
            }
        }
    </script>
    <script type="text/javascript">
        $(function () {
            var value = "", text = "";
            //$("input:checkbox").eq(0).attr("disabled", "disabled")//----------根节点变灰
            $("#Button1").click(function () {
             $("input[type='checkbox']:checked").each(function() {
            value += $(this).next().attr("title")+",";//--value值,TreeView1里要添加ToolTip的值,才能获取到
            text += $(this).next("a").text()+",";//-----循环后得到字符串形式,TEXT生成后的值是a标签里的值

         //text = $(this).attr("title");
            });
          alert(value);
           alert(text);
           window.returnValue =value +"-"+text;
            window.close();
            value = "", text = "";
            });
        });
  
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <br />
        <br />
        <asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All" ShowLines="True">
            <Nodes>
                <asp:TreeNode Text="中国" Value="1" ToolTip="1">
                    <asp:TreeNode Text="云南" Value="2" ToolTip="2">
                        <asp:TreeNode Text="思茅" Value="3" ToolTip="3">
                            <asp:TreeNode Text="宁洱" Value="6" ToolTip="6"></asp:TreeNode>
                        </asp:TreeNode>
                    </asp:TreeNode>
                    <asp:TreeNode Text="四川" Value="4" ToolTip="4">
                        <asp:TreeNode Text="文川" Value="5" ToolTip="5"></asp:TreeNode>
                    </asp:TreeNode>
                </asp:TreeNode>
            </Nodes>
        </asp:TreeView>
        <br />
        <br />
        &nbsp;<input id="Button1" type="button" value="确定" /></div>
    </form>
</body>
</html>

posted @ 2011-08-15 13:21  agen  阅读(925)  评论(0)    收藏  举报