How to add a URL to a dynamically created tree in ASP.NET?

I created a Dynamic Treeview in this Treeview, I have to add a url, can anyone give some examples ........

please im new in asp.net .........

code below ...

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

}
+3
source share
1 answer

Assuming the database column containing the URL has a name url, you first need to retrieve them from the database:

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

Then assign them to the NavigateUrl properties of your tree nodes:

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);
       }
}
+4
source

Source: https://habr.com/ru/post/1776418/


All Articles