梅花雪2.0树型结构sqlserver数据库数据绑定,IDE是VS2010,数据库是sql server2005,部分代码如下,代码实例【点击这里】下载。
效果图
mztree_c.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="mztree_c.aspx.cs" Inherits="mztree_c" %>
<html>
<head id="Head1" runat="server">
<title></title>
<script src="treescripts/jsframework.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function PushCheckItems(obj) {
var IDs = "#";
var nodes = obj.nodes;
for (var i in nodes) {
if (nodes[i].checked) {
IDs = IDs + nodes[i].id + "#";
}
}
var hf = document.getElementById('<%=hfChecked.ClientID %>');
hf.value = IDs;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50">
</td>
<td>
<div style=" 300px; height: 500px; overflow: auto;">
<script language="JavaScript" type="text/javascript">
var data = {};
<%=treeone %>
var xmlstr = "<%=tree %>";
//*
Using("System.Web.UI.WebControls.MzTreeView");
var a = new MzTreeView();
a.dataSource = data
a.loadXmlDataString(xmlstr, '0');
a.autoSort = false;
a.useCheckbox = true
a.canOperate = true;
document.write(a.render());
a.expandLevel(6);
//*/
</script>
</div>
</td>
<td width="400" valign="top">
<asp:Button ID="Button1" runat="server" Text="获得选中值" onclick="Button1_Click" />
<asp:HiddenField ID="hfChecked" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
<html>
<head id="Head1" runat="server">
<title></title>
<script src="treescripts/jsframework.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function PushCheckItems(obj) {
var IDs = "#";
var nodes = obj.nodes;
for (var i in nodes) {
if (nodes[i].checked) {
IDs = IDs + nodes[i].id + "#";
}
}
var hf = document.getElementById('<%=hfChecked.ClientID %>');
hf.value = IDs;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="800" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50">
</td>
<td>
<div style=" 300px; height: 500px; overflow: auto;">
<script language="JavaScript" type="text/javascript">
var data = {};
<%=treeone %>
var xmlstr = "<%=tree %>";
//*
Using("System.Web.UI.WebControls.MzTreeView");
var a = new MzTreeView();
a.dataSource = data
a.loadXmlDataString(xmlstr, '0');
a.autoSort = false;
a.useCheckbox = true
a.canOperate = true;
document.write(a.render());
a.expandLevel(6);
//*/
</script>
</div>
</td>
<td width="400" valign="top">
<asp:Button ID="Button1" runat="server" Text="获得选中值" onclick="Button1_Click" />
<asp:HiddenField ID="hfChecked" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
mztree_c.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class mztree_c : System.Web.UI.Page
{
public string tree = "";
public string treeone = "";
protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes["onclick"] = "PushCheckItems(a);";
if (!Page.IsPostBack)
{
string sql = "";
string connstr = System.Configuration.ConfigurationManager.AppSettings["DataSQL"];
DB cDB = new DB();
sql = "select * from mztree";
DataTable DT = cDB.RunSQLGetDT(sql, connstr);
if (DT.Rows.Count > 0)
{
tree = tree + "<nodes>";
treeone = "data['-1_0'] = 'text: 梅花雪树;';";
nodeBinding(DT, "0");
tree = tree + "</nodes>";
}
}
}
public void nodeBinding(DataTable _dt, string _filter)
{
string sFilter = "pid=" + _filter;
DataView dv = new DataView(_dt);
dv.RowFilter = sFilter;
if (dv.Count > 0)
{
foreach (DataRowView drv in dv)
{
tree = tree + "<node id='" + drv["ID"].ToString() + "' text='" + drv["name"].ToString() + "'>";
nodeBinding(_dt, drv["ID"].ToString());
tree = tree + "</node>";
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(hfChecked.Value);
Response.End();
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class mztree_c : System.Web.UI.Page
{
public string tree = "";
public string treeone = "";
protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes["onclick"] = "PushCheckItems(a);";
if (!Page.IsPostBack)
{
string sql = "";
string connstr = System.Configuration.ConfigurationManager.AppSettings["DataSQL"];
DB cDB = new DB();
sql = "select * from mztree";
DataTable DT = cDB.RunSQLGetDT(sql, connstr);
if (DT.Rows.Count > 0)
{
tree = tree + "<nodes>";
treeone = "data['-1_0'] = 'text: 梅花雪树;';";
nodeBinding(DT, "0");
tree = tree + "</nodes>";
}
}
}
public void nodeBinding(DataTable _dt, string _filter)
{
string sFilter = "pid=" + _filter;
DataView dv = new DataView(_dt);
dv.RowFilter = sFilter;
if (dv.Count > 0)
{
foreach (DataRowView drv in dv)
{
tree = tree + "<node id='" + drv["ID"].ToString() + "' text='" + drv["name"].ToString() + "'>";
nodeBinding(_dt, drv["ID"].ToString());
tree = tree + "</node>";
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(hfChecked.Value);
Response.End();
}
}