C# 如何添加URL';在ASP.NET中动态创建的树视图中?

C# 如何添加URL';在ASP.NET中动态创建的树视图中?,c#,asp.net,treeview,C#,Asp.net,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 Sy

我已经在树视图中创建了动态树视图,我必须添加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);
           }
    }

}

假设包含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);
}
然后将它们指定给树节点的属性:

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);
       }
}

你能发布你已经拥有的代码吗,这样我们就可以从那里开始了?你想添加的URL也来自数据库吗?若有,他们的列名是什么?是的,URL来自数据库。我没有添加,也不知道如何在数据库中添加.aspx文件。。。。你能给我一些吗example@Raj,您不需要将
aspx
文件添加到数据库中。为了回答您的问题,我们只需要知道存储URL的数据库列的名称:)我已经添加了3个列:only id、parentid、title。我没有在数据库中添加URL列。