2010-11-23 58 views
3

我已經在Treeview中創建了動態TreeView,我必須添加URL的,可以給任何一個例子........如何在ASP.NET中動態創建的樹狀視圖中添加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 [email protected]",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); 
      } 
    } 

} 
+0

你可以發佈你已有的代碼,所以我們可以從那裏開始? – 2010-11-23 10:27:46

回答

4

假設包含該網址的數據庫列被命名爲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 [email protected]",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); 
     } 
}