1.生成一个TREEVIEW,节点一次性全部加载
/// <summary>
/// 初始化栏目树
/// </summary>
private void initTree()
{
//得到根栏目
Channel rootChannel = CmsHttpContext.Current.RootChannel;
int dc = rootChannel.Channels.Count;
//清空原有的目录树
ChannelView.Nodes.Clear();
TreeNode rootNode = new TreeNode();
rootNode.Text = "栏目树";
rootNode.Value = rootChannel.Guid;
//rootNode.NavigateUrl = "~/Module/ChannelInfoManage/ChannelManage.aspx?Guid=" + rootChannel.Guid;
rootNode.Target = "ChannelManage";
//调用函数 用于生成子节点
CreateTree(rootChannel, rootNode);
//ChannelView为在ASPX里声明的一个TREEVIEW对象
ChannelView.Nodes.Add(rootNode);
ChannelView.ExpandDepth = 1;
}
/// <summary>
/// 创建栏目树
/// </summary>
/// <param name="rootChannel">根栏目</param>
/// <param name="rootNode">根节点</param>
public void CreateTree(Channel rootChannel, TreeNode rootNode)
{
if (rootChannel.Channels.Count > 0 && rootChannel.Channels != null)
{
foreach (Channel subChannel in rootChannel.Channels)
{
TreeNode subNode = new TreeNode();
//TREEVIEW节点中节点的显示名称
subNode.Text = subChannel.Name;
//设置节点的值
subNode.Value = subChannel.Guid;
//subNode.NavigateUrl = "~/Module/ChannelInfoManage/ChannelManage.aspx?Guid=" + subChannel.Guid;
subNode.Target = "ChannelManage";
//在节点前面是否显示复选框
subNode.ShowCheckBox = true;
//获取一个值,表示复选框是否被选中
subNode.Checked = subChannel.IsHiddenModePublished;
//根节点加入子节点
rootNode.ChildNodes.Add(subNode);
//递归调用,生成子节点
CreateTree(subChannel, subNode);
}
}
}
二:TREEVIEW的动态加载生成,点击某个节点,其下的节点动态产生。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//设置提交按钮确认属性
submitButtonInit(submitButton);
//初始化栏目树
Channel rootChannel = context.RootChannel;
TreeNode rootNode = new TreeNode();
rootNode.Text = rootChannel.Name;
rootNode.Value = rootChannel.Guid;
//设置动态填充节点
rootNode.PopulateOnDemand = true;
//设置节点为折叠
rootNode.Expanded = false;
//TREEVIEW加入了根节点
ChannelView.Nodes.Add(rootNode);
}
}
/// <summary>
/// 初始化确定按钮,在客户端确认某栏目是否被隐藏
/// </summary>
private void submitButtonInit(Button submitButton)
{
submitButton.Attributes.Add("onClick", "return confirm('请确认是否改变当前所选子栏目的隐藏状态?')");
}
//实现动态填充TREEVIEW的函数
protected void ChannelView_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
Channel curChannel = null;
ChannelCollection channelCol = null;
curChannel = context.Searches.GetByGuid(e.Node.Value) as Channel;
channelCol = curChannel.Channels;
if (channelCol != null && channelCol.Count > 0)
{
foreach (Channel subChannel in channelCol)
{
TreeNode node = new TreeNode();
node.Text = subChannel.Name;
node.Value = subChannel.Guid;
node.ShowCheckBox = true;
node.Checked = channelOperater.IsChannelHided(subChannel);
node.PopulateOnDemand = true;
node.Expanded = false;
e.Node.ChildNodes.Add(node);
}
}
}
三:捕获TREEVIEW节点前的复选框是否被选中或取消。
TREEVIEW ChannelView=new TREEVIEW();
ChannelView.CheckedNodes
treeview的CheckedNodes属性代表了被节点前的复选框选中了的节点集合TREENODECOLLECTION
至于那些取消了本来打钩的复选框的节点集合
TreeNodeCollection tc = new TreeNodeCollection();
有以下生成:
int i = 0;
protected void ChannelView_TreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
{
if (e.Node.Checked == false)
tc.Add(e.Node);
}