如何在ASP.NET中动态创建树视图中添加URL?

时间:2010-11-23 10:00:52

标签: c# asp.net treeview

我在Treeview中创建了Dynamic Treeview我必须添加URL,任何人都可以提供一些例子........

请在asp.net新增.........

下面给出的代码......

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 TreeViewCS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            PopulateRootLevel();
    }

    private void PopulateRootLevel()
    {
        SqlConnection objConn = new SqlConnection(@"server=AG-SERVER;Initial Catalog=abc;User ID=ab-cdef;Password=1234"); 
        SqlCommand objCommand=new SqlCommand(@"select id,title,(select count(*) FROM SampleCategories WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID IS NULL",objConn );
        SqlDataAdapter da=new SqlDataAdapter(objCommand); 
        DataTable dt=new DataTable();
        da.Fill(dt);
        PopulateNodes(dt,TreeView1.Nodes);
    }

    private void PopulateSubLevel(int parentid,TreeNode parentNode)
    {
        SqlConnection objConn = new SqlConnection(@"server=AG-SERVER;Initial Catalog=abc;User ID=ab-cdef;Password=1234"); 
        SqlCommand objCommand=new SqlCommand(@"select id,title,(select count(*) FROM SampleCategories WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID=@parentID",objConn );
        objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;
        SqlDataAdapter da=new SqlDataAdapter(objCommand); 
        DataTable dt=new DataTable();
        da.Fill(dt);
        PopulateNodes(dt,parentNode.ChildNodes);
    }


    protected void TreeView1_TreeNodePopulate(object sender,TreeNodeEventArgs e)
    {
        PopulateSubLevel(Int32.Parse(e.Node.Value),e.Node);   
    }

    private void PopulateNodes(DataTable dt,TreeNodeCollection nodes)
    {
           foreach( DataRow dr in dt.Rows)
           {
                TreeNode tn=new TreeNode();
                tn.Text = dr["title"].ToString();
                tn.Value = dr["id"].ToString();
                nodes.Add(tn);

                //If node has child nodes, then enable on-demand populating
                tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
           }
    }

}

1 个答案:

答案 0 :(得分:4)

假设包含该URL的数据库列名为url,您首先需要从数据库中获取它们:

private void PopulateRootLevel()
{
    SqlConnection objConn = new SqlConnection(@"server=AG-SERVER;Initial Catalog=abc;User ID=ab-cdef;Password=1234"); 
    SqlCommand objCommand=new SqlCommand(@"select id,title,url,(select count(*) FROM SampleCategories WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID IS NULL",objConn );
    SqlDataAdapter da=new SqlDataAdapter(objCommand); 
    DataTable dt=new DataTable();
    da.Fill(dt);
    PopulateNodes(dt,TreeView1.Nodes);
}

private void PopulateSubLevel(int parentid,TreeNode parentNode)
{
    SqlConnection objConn = new SqlConnection(@"server=AG-SERVER;Initial Catalog=abc;User ID=ab-cdef;Password=1234"); 
    SqlCommand objCommand=new SqlCommand(@"select id,title,url,(select count(*) FROM SampleCategories WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID=@parentID",objConn );
    objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;
    SqlDataAdapter da=new SqlDataAdapter(objCommand); 
    DataTable dt=new DataTable();
    da.Fill(dt);
    PopulateNodes(dt,parentNode.ChildNodes);
}

然后将它们分配给树节点的NavigateUrl属性:

private void PopulateNodes(DataTable dt,TreeNodeCollection nodes)
{
       foreach( DataRow dr in dt.Rows)
       {
            TreeNode tn=new TreeNode();
            tn.Text = dr["title"].ToString();
            tn.Value = dr["id"].ToString();
            tn.NavigateUrl = dr["url"].ToString();
            nodes.Add(tn);

            //If node has child nodes, then enable on-demand populating
            tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
       }
}