.aspx:
<script type="text/javascript"> function getReturnValue() { var strResult = window.showModalDialog('Depart.aspx?random=' + Math.random(), "", "dialogWidth=400px;dialogHeight=500px;status:no"); if (strResult != undefined) { var str = new Array(); str = strResult.split("|"); $("#hidId").val(str[0]); $("#hidName").val(str[1]); $("#txtDepart").val(str[1]); //alert(str[1]); } } </script> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td> 请选择: </td> <td> <asp:TextBox ID="txtDepart" runat="server" CssClass="input required" onclick="getReturnValue();" size="30" ReadOnly="True"></asp:TextBox> <asp:HiddenField ID="hidId" runat="server" /> <asp:HiddenField ID="hidName" runat="server" /> </td> </tr> </table>
.aspx.cs:只需保存hidId的值
Depart.aspx:
<link rel="stylesheet" href="css/demo.css" type="text/css"> <link rel="stylesheet" href="css/zTreeStyle/zTreeStyle.css" type="text/css"> <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="js/jquery.ztree.core-3.5.js"></script> <SCRIPT type="text/javascript" language="javascript"> var setting = { data: { key: { title: "t" }, simpleData: { enable: true } }, callback: { beforeDblClick: zTreeBeforeDblClick, onClick: onClick } }; var zNodes = <%= sb.ToString() %>; var log, className = "dark"; function beforeClick(treeId, treeNode, clickFlag) { className = (className === "dark" ? "" : "dark"); showLog("[ " + getTime() + " beforeClick ] " + treeNode.name); return (treeNode.click != false); } function zTreeBeforeDblClick(treeId, treeNode) { if (treeNode != null && treeNode != 'undefined') { $("#hidId").val(treeNode.id); $("#hidName").val(treeNode.name); window.returnValue = $("#hidId").val() + "|" + $("#hidName").val(); //alert($("#hidId").val() + "|" + $("#hidName").val()); window.close(); } } function onClick(event, treeId, treeNode, clickFlag) { //showLog("[ " + getTime() + " onClick ] clickFlag = " + clickFlag + " (" + (clickFlag === 1 ? "普通选中" : (clickFlag === 0 ? "<b>取消选中</b>" : "<b>追加选中</b>")) + ")"); //window.close(); } function showLog(str) { if (!log) log = $("#log"); log.append("<li class='" + className + "'>" + str + "</li>"); if (log.children("li").length > 8) { log.get(0).removeChild(log.children("li")[0]); } } function getTime() { var now = new Date(), h = now.getHours(), m = now.getMinutes(), s = now.getSeconds(); return (h + ":" + m + ":" + s); } $(document).ready(function() { $.fn.zTree.init($("#treeDemo"), setting, zNodes); }); </SCRIPT> <ul id="treeDemo" class="ztree"></ul> <asp:HiddenField ID="hidId" runat="server" /> <asp:HiddenField ID="hidName" runat="server" />
Depart.aspx.cs:
public StringBuilder sb = new StringBuilder(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataSet ds; string strSql = "select * from dt_Depart"; ds = ZBGA.DBUtility.DbHelperSQL.Query(strSql); sb.Append("["); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { sb.Append("{"); string id = ds.Tables[0].Rows[i]["ID"].ToString(); sb.Append("id:"" + id + """); sb.Append(","); string pId = ds.Tables[0].Rows[i]["ParentId"].ToString(); sb.Append("pId:"" + pId + """); sb.Append(","); string name = ds.Tables[0].Rows[i]["Title"].ToString(); sb.Append("name:"" + name + """); sb.Append(","); sb.Append("t:"" + name + """); sb.Append("}"); if (i != ds.Tables[0].Rows.Count - 1) { sb.Append(","); } } sb.Append("]"); } }
双击某一项后把该项的值传递给父窗口,并关闭自身