动态生成TreeView节点

来源:互联网 发布:java实现银行转账 编辑:程序博客网 时间:2024/05/26 19:20

 <asp:TreeView ID="tv_ProductCate" runat="server" OnTreeNodePopulate="tv_ProductCate_TreeNodePopulate" NodeIndent="10" ImageSet="Arrows">
    <Nodes></Nodes>
 </asp:TreeView>

public partial class Ascx_ProductCates : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e){
        if (!Page.IsPostBack)
        {
            LoadTreeLevel0();
        }
    }

    protected void tv_ProductCate_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.ChildNodes.Count == 0)
        {
            switch (e.Node.Depth)
            {
                case 0://0级
                    LoadTreeLevel1(e.Node);
                    break;
                default:
                    break;
            }
        }
    }

    /// <summary>
    /// 加载0级
    /// </summary>
    private void LoadTreeLevel0()
    {
        try
        {
            DataSet mtDs;
            ReturnValue rtn = DataRefDal.SearchMerchType(out mtDs);
            if (mtDs.Tables.Count > 0)
            {
                foreach (DataRow row in mtDs.Tables[0].Rows)
                {
                    TreeNode NewNode = new TreeNode(row["MerchType"].ToString(), row["MTypeID"].ToString());
                    //NewNode.NavigateUrl = "AddNews.aspx?Mid=" + NewNode.Value;

                    //NewNode.Target = "main";
                    //TreeNode NewNode = new TreeNode(row["cname"].ToString());
                    NewNode.Expanded = false;//折叠 节点默认是展开的,就会触发TreeNodePopulate事件
                    NewNode.PopulateOnDemand = true;
                    NewNode.SelectAction = TreeNodeSelectAction.Expand;//None;已经是叶子节点,所以不引发事件
                    //node.ChildNodes.Add(NewNode);
                    tv_ProductCate.Nodes.Add(NewNode);//触发tv_ProductCate_TreeNodePopulate事件
                }
            }
        }
        catch (Exception ex)
        {
            LogManager.WriteTextLog("ScoreExWeb", "Ascx_ProductCates", "LoadTreeLevel0", ex.Message);
        }
    
    }

    /// <summary>
    /// 加载1级
    /// </summary>
    private void LoadTreeLevel1(TreeNode node)
    {
        try
        {
            DataSet mtDs;
            ReturnValue rtn = DataRefDal.SearchCategory(int.Parse(node.Value), out mtDs);
            if (mtDs.Tables.Count > 0)
            {
                foreach (DataRow row in mtDs.Tables[0].Rows)
                {
                    TreeNode NewNode = new TreeNode(row["Category"].ToString(), row["CategoryID"].ToString());
                    NewNode.NavigateUrl = "../Shoping/ShopLists.aspx?CateID=" + NewNode.Value;

                    //NewNode.Target = "main";
                    //TreeNode NewNode = new TreeNode(row["cname"].ToString());
                    NewNode.Expanded = false;//折叠;
                    NewNode.PopulateOnDemand = true;
                    NewNode.SelectAction = TreeNodeSelectAction.Expand;//None;已经是叶子节点,所以不引发事件
                    node.ChildNodes.Add(NewNode);
                }

            }
        }
        catch (Exception ex)
        {
            LogManager.WriteTextLog("ScoreExWeb", "Ascx_ProductCates", "LoadTreeLevel1", ex.Message);
        }
      
    }
}

 

原创粉丝点击