这个案例的需求是必须要做checkBox的联动,所以俺就使用了yycontrols的treeview控件:)
前台:
<yyc:smarttreeview id="SmartTreeView1" runat="server"
AllowCascadeCheckbox="true"></yyc:smarttreeview>
AllowCascadeCheckbox="true"></yyc:smarttreeview>
后台:
初始化、读取
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//treeview init
BindTree(SmartTreeView1.Nodes, "0");
string[] strCheckedArray = ds.Tables[0].Rows[0]["mailTo"].ToString().Split(',');
foreach (TreeNode tn in SmartTreeView1.Nodes)
{
readNotes(tn,strCheckedArray );
void BindTree(TreeNodeCollection nds, string parentId)
{
TreeNode tn = null;
DataTable dt = GetTreeTable();
foreach (DataRow dr in dt.Select("[parentId]='" + parentId+"'"))
{
tn = new TreeNode(dr["ID"].ToString(), dr["id"].ToString());
tn.ShowCheckBox = true;
tn.SelectAction = TreeNodeSelectAction.None;
tn.CollapseAll();
//扩展DTS
if (parentId == "0")
{
tn.Expand();
}
}
}
public DataTable GetTreeTable()
{
DataTable dt = new DataTable();
HttpContext.Current.Cache.Insert("Treeview", dt);
return dt;
}
//读取
void readNotes(TreeNode tn,string[] strCheckedArray)
{
foreach (TreeNode tnChild in tn.ChildNodes)
{
foreach (string strChecked in strCheckedArray)
{
if (strChecked == tnChild.Value)
{
tnChild.Checked = true;
parentExpand(tnChild);
}
}
readNotes(tnChild,strCheckedArray );
}
}
//扩展TreeView
void parentExpand(TreeNode tnChild)
{
if (tnChild.Parent.Depth != 0)
{
tnChild.Parent.Expand();
parentExpand(tnChild.Parent);
}
}
{
if (!IsPostBack)
{
//treeview init
BindTree(SmartTreeView1.Nodes, "0");
//treeview read
string[] strCheckedArray = ds.Tables[0].Rows[0]["mailTo"].ToString().Split(',');
foreach (TreeNode tn in SmartTreeView1.Nodes)
{
readNotes(tn,strCheckedArray );
}
}
void BindTree(TreeNodeCollection nds, string parentId)
{
TreeNode tn = null;
DataTable dt = GetTreeTable();
foreach (DataRow dr in dt.Select("[parentId]='" + parentId+"'"))
{
tn = new TreeNode(dr["ID"].ToString(), dr["id"].ToString());
tn.ShowCheckBox = true;
nds.Add(tn);
BindTree(tn.ChildNodes,dr[
"id"].ToString());tn.SelectAction = TreeNodeSelectAction.None;
tn.CollapseAll();
//扩展DTS
if (parentId == "0")
{
tn.Expand();
}
}
}
public DataTable GetTreeTable()
{
DataTable dt = new DataTable();
dt
= db.GetDataTable("select id,parentid from trvPerson");HttpContext.Current.Cache.Insert("Treeview", dt);
return dt;
}
//读取
void readNotes(TreeNode tn,string[] strCheckedArray)
{
foreach (TreeNode tnChild in tn.ChildNodes)
{
foreach (string strChecked in strCheckedArray)
{
if (strChecked == tnChild.Value)
{
tnChild.Checked = true;
parentExpand(tnChild);
}
}
readNotes(tnChild,strCheckedArray );
}
}
//扩展TreeView
void parentExpand(TreeNode tnChild)
{
if (tnChild.Parent.Depth != 0)
{
tnChild.Parent.Expand();
parentExpand(tnChild.Parent);
}
}
存储:
//get TreeView Nodes
StringBuilder sb = new StringBuilder();
foreach (TreeNode tn in SmartTreeView1.CheckedNodes)
{
sb.Append(tn.Value + ",");
StringBuilder sb = new StringBuilder();
foreach (TreeNode tn in SmartTreeView1.CheckedNodes)
{
sb.Append(tn.Value + ",");
}
数据库结构:
USE [VS2005]
GO
/****** 对象: Table [dbo].[trvPerson] 脚本日期: 03/21/2008 13:18:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[trvPerson](
[id] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[parentID] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[tableName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[url] [nvarchar](max) COLLATE Chinese_PRC_CI_AS NULL,
[target] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[expanded] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
/****** 对象: Table [dbo].[trvPerson] 脚本日期: 03/21/2008 13:18:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[trvPerson](
[id] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[parentID] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[tableName] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[url] [nvarchar](max) COLLATE Chinese_PRC_CI_AS NULL,
[target] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[expanded] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
相关文章
暂无评论...