在ASP.NET中ShowModalDialog+ztree的使用

时间:2023-03-09 13:40:08
在ASP.NET中ShowModalDialog+ztree的使用

.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[]);
$("#hidName").val(str[]);
$("#txtDepart").val(str[]);
//alert(str[1]);
}
}
</script> <table width="100%" border="" cellspacing="" cellpadding="">
<tr>
<td>
请选择:
</td>
<td>
<asp:TextBox ID="txtDepart" runat="server" CssClass="input required" onclick="getReturnValue();" size="" 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 ]&nbsp;&nbsp;" + 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 ]&nbsp;&nbsp;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 > ) {
log.get().removeChild(log.children("li")[]);
}
}
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 = ; i < ds.Tables[].Rows.Count; i++)
{
sb.Append("{");
string id = ds.Tables[].Rows[i]["ID"].ToString();
sb.Append("id:\"" + id + "\"");
sb.Append(",");
string pId = ds.Tables[].Rows[i]["ParentId"].ToString();
sb.Append("pId:\"" + pId + "\"");
sb.Append(",");
string name = ds.Tables[].Rows[i]["Title"].ToString();
sb.Append("name:\"" + name + "\"");
sb.Append(",");
sb.Append("t:\"" + name + "\"");
sb.Append("}");
if (i != ds.Tables[].Rows.Count - )
{
sb.Append(",");
}
}
sb.Append("]");
}
}

双击某一项后把该项的值传递给父窗口,并关闭自身

在ASP.NET中ShowModalDialog+ztree的使用