前台代码:
<link rel="stylesheet" href="../css/zTreeStyle/zTreeStyle.css" type="text/css"> <script type="text/javascript" src="../script/jquery-1.4.4.min.js"></script> <script type="text/javascript" src="../script/jquery.ztree.core-3.5.js"></script> <script type="text/javascript" src="../script/jquery.ztree.excheck-3.5.js"></script> <script language="javascript" type="text/javascript"> var setting = { check: { enable: true, chkboxType: {"Y":"", "N":""} }, view: { dblClickExpand: false }, data: { simpleData: { enable: true } }, callback: { beforeClick: beforeClick, onCheck: onCheck } }; var zNodes =<%= seriesData.ToString() %>; function beforeClick(treeId, treeNode) { var zTree = $.fn.zTree.getZTreeObj("treeDemo"); zTree.checkNode(treeNode, !treeNode.checked, null, true); return false; } function onCheck(e, treeId, treeNode) { var zTree = $.fn.zTree.getZTreeObj("treeDemo"), nodes = zTree.getCheckedNodes(true), v = ""; var hidSaveId = ""; for (var i=0, l=nodes.length; i<l; i++) { v += nodes[i].name + ","; hidSaveId += nodes[i].id + ","; } if (v.length > 0 ) v = v.substring(0, v.length-1); var cityObj = $("#txtProPlayers"); cityObj.attr("value", v); var hidObj = $("#hidSaveId"); hidObj.attr("value",hidSaveId); } function showMenu() { var cityObj = $("#txtProPlayers"); var cityOffset = $("#txtProPlayers").offset(); $("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast"); $("body").bind("mousedown", onBodyDown); } function hideMenu() { $("#menuContent").fadeOut("fast"); $("body").unbind("mousedown", onBodyDown); } function onBodyDown(event) { if (!(event.target.id == "menuBtn" || event.target.id == "txtProPlayers" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) { hideMenu(); } } $(document).ready(function(){ $.fn.zTree.init($("#treeDemo"), setting, zNodes); }); function proSave() { var proname = document.getElementById(‘txtProName‘).value; if(proname.length < 1) { alert(‘项目名称不能为空!‘); return false; } else { return true; } } </SCRIPT> <asp:TextBox ID="txtProPlayers" ReadOnly="true" runat="server" Width="300px" onclick="showMenu();" ></asp:TextBox> <div id="menuContent" class="menuContent" style="display:none; position: absolute;"> <ul id="treeDemo" class="ztree" style="margin-top:0; width:180px; height: 300px;"></ul> </div> <asp:HiddenField ID="hidSaveId" runat="server" />
后台代码:
public StringBuilder seriesData = new StringBuilder(); public DataSet ds1; public DataSet ds2; protected void Page_Load(object sender, EventArgs e) { string cmdstr = ""; string cmdstr1 = ""; string name = ""; string no = ""; string departID = ""; string parentID = ""; cmdstr = "select * from tb_Depart"; ds1 = PM.DBUtility.DbHelperSQL.Query(cmdstr); seriesData.Append("["); for (int i = 0; i < ds1.Tables[0].Rows.Count; i++) { seriesData.Append("{"); name = ds1.Tables[0].Rows[i]["DepartName"].ToString(); seriesData.Append("name:\"" + name + "\""); seriesData.Append(","); no = ds1.Tables[0].Rows[i]["ID"].ToString(); seriesData.Append("id:" + no); seriesData.Append(","); parentID = ds1.Tables[0].Rows[i]["ParentID"].ToString(); //seriesData.Append("pId:" + parentID); seriesData.Append("pId:0"); seriesData.Append(","); seriesData.Append("open:true,nocheck:true}"); if (i != ds1.Tables[0].Rows.Count - 1) { seriesData.Append(","); } cmdstr1 = "select * from tb_User where DepartID=‘" + no + "‘"; ds2 = PM.DBUtility.DbHelperSQL.Query(cmdstr1); for (int j = 0; j < ds2.Tables[0].Rows.Count; j++) { seriesData.Append("{"); name = ds2.Tables[0].Rows[j]["UserRealName"].ToString(); seriesData.Append("name:\"" + name + "\""); seriesData.Append(","); no = ds2.Tables[0].Rows[j]["ID"].ToString(); seriesData.Append("id:" + no); seriesData.Append(","); departID = ds2.Tables[0].Rows[j]["DepartID"].ToString(); seriesData.Append("pId:" + departID); seriesData.Append("}"); if (j != ds2.Tables[0].Rows.Count) { seriesData.Append(","); } } } seriesData.Append("]"); } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public static DataSet Query(string SQLString) { using (SqlConnection connection = new SqlConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); SqlDataAdapter command = new SqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (System.Data.SqlClient.SqlException ex) { return ds; throw new Exception(ex.Message); } return ds; } } /// <summary> /// 删除最后结尾的一个逗号 /// </summary> public static string DelLastComma(string str) { return str.Substring(0, str.LastIndexOf(",")); }