http://download1.csdn.net/down3/20070515/15155625734.rar 想了一下,觉得还是可以用JS完全控制2005TreeView中的CheckBox,包括父节点选中时子节点全选、同级节点的互斥,也就是同级节点只能选择一个。。。 为了抛砖引玉,在此实现同级节点互斥。思路很简单,绑定数时同时生成一字符序列记住父子关系,客户端就根据此序列做操作。
using
System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
if (!Page.IsPostBack)
...{
ViewState["treenodes"] = string.Empty;
ViewState["trigger"] = 0;
BindTree();
this.tnodes.Value = "(" + ViewState["treenodes"].ToString() + ")";
}
}
private void BindTree()
...{
DataTable dt = getChildNodes("0");
for (int i = 0; i < dt.Rows.Count; i++)
...{
TreeNode tn = new TreeNode();
tn.Text = dt.Rows[i]["SM_Name"].ToString();
//绑定数的同时生成父子节点关系字符串
string treenodes = ViewState["treenodes"].ToString();
string trigger = ViewState["trigger"].ToString();
treenodes += trigger + "(";
ViewState["treenodes"] = treenodes;
ViewState["trigger"] = Convert.ToInt32(ViewState["trigger"].ToString()) + 1;
this.TreeView1.Nodes.Add(tn);
AddTreeNode(dt.Rows[i]["SM_ID"].ToString(), tn);
}
}
private void AddTreeNode(string ID, TreeNode par)
...{
DataTable dt = getChildNodes(ID);
if (dt.Rows.Count == 0) return;
for (int i = 0; i < dt.Rows.Count; i++)
...{
TreeNode tn = new TreeNode();
tn.Text = dt.Rows[i]["SM_Name"].ToString();
par.ChildNodes.Add(tn);
string treenodes = ViewState["treenodes"].ToString();
string trigger = ViewState["trigger"].ToString();
DataTable dts = getChildNodes(dt.Rows[i]["SM_ID"].ToString());
if (dts.Rows.Count == 0 && i == dt.Rows.Count - 1) ...{ treenodes += trigger + ")"; }
else if (dts.Rows.Count == 0 && i < dt.Rows.Count - 1) ...{ treenodes += trigger + ","; }
else ...{ treenodes += trigger + "("; }
ViewState["treenodes"] = treenodes;
ViewState["trigger"] = Convert.ToInt32(ViewState["trigger"].ToString()) + 1;
AddTreeNode(dt.Rows[i]["SM_ID"].ToString(), tn);
}
}
private DataTable getChildNodes(string PID)
...{
SqlConnection conn = new SqlConnection("server=localhost;database=TreeViewSource;uid=sa;pwd=sa;trusted_connection=true");
SqlDataAdapter sda = new SqlDataAdapter("select * from SYS_Module where SM_PID=" + PID, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
if (!Page.IsPostBack)
...{
ViewState["treenodes"] = string.Empty;
ViewState["trigger"] = 0;
BindTree();
this.tnodes.Value = "(" + ViewState["treenodes"].ToString() + ")";
}
}
private void BindTree()
...{
DataTable dt = getChildNodes("0");
for (int i = 0; i < dt.Rows.Count; i++)
...{
TreeNode tn = new TreeNode();
tn.Text = dt.Rows[i]["SM_Name"].ToString();
//绑定数的同时生成父子节点关系字符串
string treenodes = ViewState["treenodes"].ToString();
string trigger = ViewState["trigger"].ToString();
treenodes += trigger + "(";
ViewState["treenodes"] = treenodes;
ViewState["trigger"] = Convert.ToInt32(ViewState["trigger"].ToString()) + 1;
this.TreeView1.Nodes.Add(tn);
AddTreeNode(dt.Rows[i]["SM_ID"].ToString(), tn);
}
}
private void AddTreeNode(string ID, TreeNode par)
...{
DataTable dt = getChildNodes(ID);
if (dt.Rows.Count == 0) return;
for (int i = 0; i < dt.Rows.Count; i++)
...{
TreeNode tn = new TreeNode();
tn.Text = dt.Rows[i]["SM_Name"].ToString();
par.ChildNodes.Add(tn);
string treenodes = ViewState["treenodes"].ToString();
string trigger = ViewState["trigger"].ToString();
DataTable dts = getChildNodes(dt.Rows[i]["SM_ID"].ToString());
if (dts.Rows.Count == 0 && i == dt.Rows.Count - 1) ...{ treenodes += trigger + ")"; }
else if (dts.Rows.Count == 0 && i < dt.Rows.Count - 1) ...{ treenodes += trigger + ","; }
else ...{ treenodes += trigger + "("; }
ViewState["treenodes"] = treenodes;
ViewState["trigger"] = Convert.ToInt32(ViewState["trigger"].ToString()) + 1;
AddTreeNode(dt.Rows[i]["SM_ID"].ToString(), tn);
}
}
private DataTable getChildNodes(string PID)
...{
SqlConnection conn = new SqlConnection("server=localhost;database=TreeViewSource;uid=sa;pwd=sa;trusted_connection=true");
SqlDataAdapter sda = new SqlDataAdapter("select * from SYS_Module where SM_PID=" + PID, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
return dt;
}
}
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
< head id ="Head1" runat ="server" >
< title > Untitled Page </ title >
< script type ="text/javascript" > ...
var tnodes = "";
function window.onload()
...{
//取得TreeView父子节点关系序列-类似与 (0(1,2)3(4(5)6)7(8)9(10))
tnodes = document.getElementById("tnodes").value;
}
function FCheck()
...{
var ocheck = event.srcElement;
if(ocheck.tagName == "INPUT"&& ocheck.type=="checkbox")
...{
//取得该CheckBox在序列中对应的数字
var t = ocheck.id.replace("TreeView1n","").replace("CheckBox","");
//得到同级节点
var fid = getFNodes(t);
//互斥操作判断
if(ocheck.checked==true)
...{
for(var i=0;i<fid.length;i++)
...{
if(fid[i]==t)continue;
else
...{
document.getElementById("TreeView1n"+fid[i]+"CheckBox").checked = false;
}
}
}
}
}
function getFNodes(num)
...{
//在tnodes序列中找到num
var split = tnodes.indexOf(num,0);
var pre = tnodes.substring(0,split);
var nex = tnodes.substring(split+1,tnodes.length);
//截出包含所有同级节点的字符串
var nid = getSplit(tnodes.substring(getLeftBracket(pre),getRightBracket(nex)));
var fid = new Array();
for(var j=0;j<nid.length;j++)
...{
if(nid[j].length>0)
...{
fid.push(nid[j]);
}
}
return fid;
}
//取得字符串自右向左第一个不匹配"("的index
function getLeftBracket(str)
...{
var tag = 0;
for(var i=str.length-1;i>-1;i--)
...{
if(str.charAt(i) ==')')...{tag=tag-1;continue;}
else if(str.charAt(i)=='(')
...{
if(tag!=0)...{tag=tag+1;continue;}
else if(tag==0)...{return i;}
}
}
}
//取得字符串自左向右第一个不匹配")"的index
function getRightBracket(str)
...{
var tag = 0;
for(var i=0;i<str.length;i++)
...{
if(str.charAt(i) =='(')...{tag=tag-1;continue;}
else if(str.charAt(i)==')')
...{
if(tag!=0)...{tag=tag+1;continue;}
else if(tag==0)...{return(i+tnodes.length-str.length+1);}
}
}
}
//取得与目标节点的同级节点标号
function getSplit(str)
...{
var splitstr = new Array();
while(str.indexOf("(",1)>-1)
...{
var tem = str.substring(str.indexOf("(",1)+1,str.length);
str = str.replace(str.substring(str.indexOf("(",1),getRightB(tem,str)),"|");
}
var st = str.substring(1,str.length-1).replace(",","|");
var nid = st.split('|');
return nid;
}
function getRightB(rstr,fullstr)
...{
var tag = 0;
for(var i=0;i<rstr.length;i++)
...{
if(rstr.charAt(i) =='(')...{tag=tag-1;continue;}
else if(rstr.charAt(i)==')')
...{
if(tag!=0)...{tag=tag+1;continue;}
else if(tag==0)...{return(i+fullstr.length-rstr.length+1);}
}
}
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:TreeView onclick ="javascript:FCheck()" ShowCheckBoxes ="all" ID ="TreeView1" runat ="server" >
</ asp:TreeView >
< input id ="tnodes" type ="hidden" runat ="server" />
</ form >
</ body >
</ html >
< head id ="Head1" runat ="server" >
< title > Untitled Page </ title >
< script type ="text/javascript" > ...
var tnodes = "";
function window.onload()
...{
//取得TreeView父子节点关系序列-类似与 (0(1,2)3(4(5)6)7(8)9(10))
tnodes = document.getElementById("tnodes").value;
}
function FCheck()
...{
var ocheck = event.srcElement;
if(ocheck.tagName == "INPUT"&& ocheck.type=="checkbox")
...{
//取得该CheckBox在序列中对应的数字
var t = ocheck.id.replace("TreeView1n","").replace("CheckBox","");
//得到同级节点
var fid = getFNodes(t);
//互斥操作判断
if(ocheck.checked==true)
...{
for(var i=0;i<fid.length;i++)
...{
if(fid[i]==t)continue;
else
...{
document.getElementById("TreeView1n"+fid[i]+"CheckBox").checked = false;
}
}
}
}
}
function getFNodes(num)
...{
//在tnodes序列中找到num
var split = tnodes.indexOf(num,0);
var pre = tnodes.substring(0,split);
var nex = tnodes.substring(split+1,tnodes.length);
//截出包含所有同级节点的字符串
var nid = getSplit(tnodes.substring(getLeftBracket(pre),getRightBracket(nex)));
var fid = new Array();
for(var j=0;j<nid.length;j++)
...{
if(nid[j].length>0)
...{
fid.push(nid[j]);
}
}
return fid;
}
//取得字符串自右向左第一个不匹配"("的index
function getLeftBracket(str)
...{
var tag = 0;
for(var i=str.length-1;i>-1;i--)
...{
if(str.charAt(i) ==')')...{tag=tag-1;continue;}
else if(str.charAt(i)=='(')
...{
if(tag!=0)...{tag=tag+1;continue;}
else if(tag==0)...{return i;}
}
}
}
//取得字符串自左向右第一个不匹配")"的index
function getRightBracket(str)
...{
var tag = 0;
for(var i=0;i<str.length;i++)
...{
if(str.charAt(i) =='(')...{tag=tag-1;continue;}
else if(str.charAt(i)==')')
...{
if(tag!=0)...{tag=tag+1;continue;}
else if(tag==0)...{return(i+tnodes.length-str.length+1);}
}
}
}
//取得与目标节点的同级节点标号
function getSplit(str)
...{
var splitstr = new Array();
while(str.indexOf("(",1)>-1)
...{
var tem = str.substring(str.indexOf("(",1)+1,str.length);
str = str.replace(str.substring(str.indexOf("(",1),getRightB(tem,str)),"|");
}
var st = str.substring(1,str.length-1).replace(",","|");
var nid = st.split('|');
return nid;
}
function getRightB(rstr,fullstr)
...{
var tag = 0;
for(var i=0;i<rstr.length;i++)
...{
if(rstr.charAt(i) =='(')...{tag=tag-1;continue;}
else if(rstr.charAt(i)==')')
...{
if(tag!=0)...{tag=tag+1;continue;}
else if(tag==0)...{return(i+fullstr.length-rstr.length+1);}
}
}
}
</ script >
</ head >
< body >
< form id ="form1" runat ="server" >
< asp:TreeView onclick ="javascript:FCheck()" ShowCheckBoxes ="all" ID ="TreeView1" runat ="server" >
</ asp:TreeView >
< input id ="tnodes" type ="hidden" runat ="server" />
</ form >
</ body >
</ html >