按以下代码扩展的TreeView,无论怎么设置ExpandDepth属性,都是折叠的,那位高手看一下是那的问题?如何解决?我想让其在默认情况下展开所有节点.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SamWebControlLib
{
    [DefaultProperty(
"Text")]
    [ToolboxData(
"<{0}:PowerTreeView runat=server></{0}:PowerTreeView>")]
    
public class PowerTreeView : TreeView
    
{
        
属性
        
/// <summary> 绑定
        
/// </summary>

        public void PowerTreeViewDataBind()
        
{
            
base.DataBind();
            
this.Create_Tree(this.DataSource);
        }


        
/// <summary>
        
/// 构造树
        
/// </summary>
        
/// <param name="source"></param>

        private void Create_Tree(Object source)
        
{
            DataTable dt 
= null;
            
if ((source as DataSet) != null)
            
{
                dt 
= ((DataSet)source).Tables[0];
            }

            
else if ((source as DataTable) != null)
            
{
                dt 
= ((DataTable)source);
            }

            
else
            
{
                
throw new Exception("只允许绑定DataSet和DataTable");
            }

            
if (dt != null && dt.Rows.Count > 0)
            
{
                
if (this.DataTextField == String.Empty) throw new ArgumentNullException("DataTextField属性不能为空!");
                
if (this.DataValueField == String.Empty) throw new ArgumentNullException("DataValueField属性不能为空!");
                
if (this.ParentIDField == String.Empty) throw new ArgumentNullException("ParentIDField属性不能为空!");
                
if (this.RootValue == String.Empty) throw new ArgumentNullException("RootValue属性不能为空!");
                
this.DataBindTreeView(null, dt, String.Format("{0}='{1}'"this.DataValueField, this.RootValue));
            }

        }

        
/// <summary>
        
/// 增加一级菜单
        
/// </summary>
        
/// <param name="CurrentNode"></param>
        
/// <param name="dt"></param>
        
/// <param name="sWhere"></param>

        private void DataBindTreeView(TreeNode CurrentNode, DataTable dt, string sWhere)
        
{
            
//string Text = "V_MENU_MC", Value = "V_MENU_DM", ToolTip = "V_MENU_EW", Menu_Top = "V_MENU_DM_SJ", NodeImg = "V_MENU_IMAGE", WHERE = "V_WHERE", Sort = "I_SORT", Url = "V_FILE";
            DataView dv = new DataView(dt);
            dv.RowFilter 
= sWhere;
            dv.Sort 
= SortField;
            
//if (CurrentNode != null && CurrentNode.ImageUrl != "images/root.gif")
            
//{
            
//    if (dv.Count == 0)
            
//    {
            
//        CurrentNode.ImageUrl = "images/last.GIF";
            
//    }
            
//    else
            
//    {
            
//        CurrentNode.ImageUrl = "images/book.gif";
            
//    }
            
//}
            foreach (DataRowView row in dv)
            
{
                TreeNode item 
= new TreeNode();
                
if (row[this.DataTextField] != DBNull.Value) item.Text = row[this.DataTextField].ToString();
                
//if (row[ToolTip] != DBNull.Value) item.ToolTip = row[ToolTip].ToString();
                string val = row[this.DataValueField].ToString();
                
if (row[this.DataValueField] != DBNull.Value) item.Value = val;
                
//if (row[Url] != DBNull.Value) item.NavigateUrl = row[Url].ToString();
                
//if (row[WHERE] != DBNull.Value) item.NavigateUrl += row[WHERE].ToString();
                
//item.Target = "main";

                
if (CurrentNode == null)
                
{
                    
//item.ImageUrl = "images/root.gif";
                    this.Nodes.Add(item);
                }

                
else
                
{
                    CurrentNode.ChildNodes.Add(item);
                }


                
string str = String.Format("{0}='{1}'"this.ParentIDField, row[this.DataValueField].ToString());
                
//item..Style.Font.Name = "楷体_GB2312";
                
//item.Style.Font.Size = new FontUnit("16px");
                
//递归
                DataBindTreeView(item, dt, str);
            }

        }

        
/// <summary>
        
/// 获取节点的值
        
/// </summary>
        
/// <param name="node"></param>
        
/// <returns></returns>

        public string GetNodeValue(TreeNode node)
        
{
            
if (node.Value == "01")
            
{
                
return "0"
            }

            
else
            
{
                
return node.Value;
            }

        }

    }

}


使用item.Expanded = true;方法,只能在控件里面修改属性,以达到展开的显示.我的想法是在控件外面可以方便使用用ExpandDepth属性或ExpandAll方法,操作控件展开折叠.再次请教,有没有办法实现?
另,如果完善了些控件,别忘了通知我.我学习一下代码
posted on 2007-09-11 11:34  网碾平  阅读(851)  评论(1编辑  收藏  举报