2010-11-23 3 views
3

Treeview에서 Dynamic 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

당신은 당신이 이미 가지고있는 코드를 게시 할 수, 그래서 우리는 거기에서 시작? –

+0

코드를 게시했습니다. – Raj

+0

추가하려는 URL도 데이터베이스에서 제공됩니까? 그렇다면 열 이름이 무엇입니까? –

답변

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 [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); 
     } 
} 
관련 문제