Add querystring to tree node

I have a page written in C# in which I am dynamically populating Nodes in a TreeView.  I used the example at http://www.15seconds.com/issue/041117.htm as my guide.  This is working perfectly but now I need to allow the user to click on a node and have him passed to a new page along with a querystring.

To complicate the matter, this link needs to appear only on the lowest level of node.  My TreeView appears like this:

Test product A
Test product B
     Sub Product i
     Sub Product ii
Test product C

Text product A  & Test product C both need to have this link.  Test product B should not have a link.  In its case the next node down needs to have the links: Sub Product i & Sub Product ii

This is my first ASP.Net project so I may need a pretty comprehensive explanation.  

<%@ Page Language="C#" MasterPageFile="~/Root.master" Title="" %>
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<%@ Import Namespace="System.Configuration"%>
<script runat="server">  
private void Page_Load(object sender, System.EventArgs e)
    {
        if (!String.IsNullOrEmpty(Request.QueryString["ID"]))
            ID = Request.QueryString["ID"].ToString();
        else
            ID = "1";
    }
    
         
void Node_Populate(object sender, System.Web.UI.WebControls.TreeNodeEventArgs e)
        {
            if(e.Node.ChildNodes.Count == 0)
            {            
                switch( e.Node.Depth )
                {  
                    case 0:                
                        FillProducts(e.Node);    
                        break;            
                    case 1:
                        FillTitlesForProducts(e.Node);
                        break;        
                }    
            }            
        }
 
       void FillProducts(TreeNode node)
        {
            string connString = ConfigurationManager.ConnectionStrings["CFCMConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(connString);
            //SqlCommand command = new SqlCommand("SELECT ID, ProductName FROM CFCM_Products WHERE CatID = @ID ORDER BY shortname", connection);
            SqlCommand command = new SqlCommand("SelectProductsFromCategory", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@ID", SqlDbType.VarChar)).SqlValue = ID;  
                   
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet Products = new DataSet();
            adapter.Fill(Products);            
 
            if (Products.Tables.Count > 0)
            {
                foreach (DataRow row in Products.Tables[0].Rows)
                {
                    TreeNode newNode = new TreeNode(row["ProductName"].ToString(), row["id"].ToString());
                    newNode.PopulateOnDemand = true;
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                    node.ChildNodes.Add(newNode);
                }
            }
        }
 
 
        void FillTitlesForProducts(TreeNode node)
        {
            string authorID = node.Value;
            string connString = ConfigurationManager.ConnectionStrings["CFCMConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("SELECT T1.ID, SubProductName FROM CFCM_SubProducts T1" + " INNER JOIN CFCM_Products T2 ON T1.ProductID = T2.ID " + " WHERE T2.ID = " + authorID + " ORDER BY T2.shortname", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet titlesForProducts = new DataSet();
            adapter.Fill(titlesForProducts);
 
            if (titlesForProducts.Tables.Count > 0)
            {
                foreach (DataRow row in titlesForProducts.Tables[0].Rows)
                {
                    TreeNode newNode = new TreeNode(row["SubProductName"].ToString(), row["id"].ToString());
                    newNode.PopulateOnDemand = true;
                    newNode.SelectAction = TreeNodeSelectAction.Expand;
                    node.ChildNodes.Add(newNode);
                }
            }
        }    
</script>
 
<asp:TreeView Runat="Server" OnTreeNodePopulate="Node_Populate" 
 ID="tvwProducts" BorderStyle="None" ImageSet="Arrows"> 
   <Nodes>
      <asp:TreeNode Text="Products" PopulateOnDemand="true" SelectAction="None"/>
   </Nodes> 
</asp:TreeView>

Open in new window

cgcmqAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
This is how I would change your code (many changes):

Bob
    private void FillTitlesForProducts(TreeNode node)
    {
        string connString = ConfigurationManager.ConnectionStrings["CFCMConnectionString"].ConnectionString;
        using (SqlDataAdapter adapter = new SqlDataAdapter("SelectProductsFromCategory", connString))
        {
            adapter.SelectCommand.Parameters.AddWithValue("@ID", ID);
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
 
            DataTable Products = new DataTable();
            adapter.Fill(Products);
 
            foreach (DataRow row in Products.Rows)
            {
                TreeNode newNode = new TreeNode(row["ProductName"].ToString(), row["id"].ToString());
                newNode.PopulateOnDemand = true;
                newNode.NavigateUrl = "Product.aspx?id=" + newNode.Value;
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                node.ChildNodes.Add(newNode);
            }
        }
    }

Open in new window

0
cgcmqAuthor Commented:
Is your code intended to replace all 3 functions that I had: Node_Populate, FillProducts & FillTitlesForProducts?

0
Bob LearnedCommented:
No, that is only a single method example to show you how you can achieve the result you need, and to show some new things, like using blocks, and the fact that you don't need an SqlConnection or SqlCommand with an SqlDataAdapter.

Bob
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

cgcmqAuthor Commented:
I added :
newNode.NavigateUrl = "Product.aspx?id=" + newNode.Value;
to both levels of the node (Products & Sub Products) and that works great.  

Is there a way that I can alter the code so that if the Product has a sub product the product node does not include the link?

Currently the SP for the products node returns:
1      1      Test product A
2      1      Test product B
3      1      Test product C

I could alter the SP so that it returned:
1      1      Test product A      0
2      1      Test product B      1
2      1      Test product B      2
3      1      Test product C      0

How would I then use the 0 in a conditional statement to control whether or not I included the link?  
0
Bob LearnedCommented:
Try something like this:

if (int.Parse(row["SubProductCount"].ToString()) > 0)
    newNode.NavigateUrl = "Product.aspx?id=" + newNode.Value;
else
    newNode.NavigateUrl = "Product.aspx";

Bob
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cgcmqAuthor Commented:
Excellent!

I modified the NavigateUrl  slightly to meet my needs and it works like a charm.

if (int.Parse(row["SubID"].ToString()) > 0)
    'this reloads the page using the querystring value
    newNode.NavigateUrl = "Products.aspx?ID=" + ID;
else
   'this passes on to the next page
   'PLevel informs Companies.aspx that the ID is a product vice a sub product
    newNode.NavigateUrl = "Companies.aspx?PLevel=0&ID=" + newNode.Value;
0
cgcmqAuthor Commented:
Thanks for all the help.  I am struggling with the transition from classic ASP to ASP.net.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.