troubleshooting Question

TreeView created dynamically in the code having issues in asp.net, c#?

Avatar of Saroj13
Saroj13 asked on
C#ASP.NET
1 Comment1 Solution1611 ViewsLast Modified:
I need to create a custom Treeview dynamically in the code and then attach to the panel. I am able to display the treeview. Issues:
1. When clicking on any node, it does:
     It shows Javascript error. Also node hover shows javascript:__dopostback()...If I click on the node, it  navigates me to the separate page.

----To avoid navigation to the next page:
   productNode.NavigateUrl = "javascript:void(0);";
This stop the navigation.

2. On node click, its not firing the SelectedNodeChanged event?

Please check my code and fix it:
[b]Webform.aspx[/b]
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="ProductGroup.aspx.cs" Inherits="ProductGroup" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <asp:Panel ID="Panel1" runat="server" Height="300px" ScrollBars="Both">
    </asp:Panel>
    <hr />
  
    </div>
    
    </form>
</body>
</html>

[b]ProductGroup.aspx.cs[/b]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProductGroup : System.Web.UI.Page
{
    PopulateTree objCatTree = new PopulateTree();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateTree.xPos = 0;
            objCatTree.PopulateProductGroups(Panel1);

  
        }
           }
   
}


[b]PopulateTree Class[/b]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.UI;




/// <summary>
/// Summary description for CategoryTree
/// </summary>
public class PopulateTree
{
    private string connectionString = ConfigurationManager.ConnectionStrings["DBConnect"].ConnectionString;

    public static int xPos = 0;
    public static bool alreadyCreated = false;
    
    public PopulateTree()
    {
        //
        // TODO: Add constructor logic here
        //


    }
    public void PopulateProductGroups(Panel treePanel)
    {
        int iCatID = 0;
        string sCatName = "";

        int iSubCatID = 0;
        string sSubCatName = "";
        
        TreeNode productNode;
        TreeNode subProdunctNode;
        TreeView productGroupsTreeView;
        SqlDataReader rdrProduct = GetProducts();

        if (rdrProduct.HasRows)
        {
            while (rdrProduct.Read())
            {
                iCatID = Convert.ToInt32(rdrProduct["ProductGroup_ID"].ToString());
                sCatName = rdrProduct["ProductGroupName"].ToString();


                productGroupsTreeView = new TreeView();

                productGroupsTreeView.ID = sCatName;
                productNode = new TreeNode();
                productNode.Value = rdrProduct["ProductGroup_ID"].ToString();
               productNode.Text = sCatName;
             
               productNode.NavigateUrl = "javascript:void(0);";
               productNode.SelectAction = TreeNodeSelectAction.SelectExpand;
               productGroupsTreeView.Target = "_self";
                                                            
                productGroupsTreeView.Nodes.Add(productNode);
                productGroupsTreeView.SelectedNodeChanged += new EventHandler(this.productGroupsTreeView_SelectedNodeChanged);
                
                treePanel.Controls.Add(productGroupsTreeView);
                SetTreePosition(productGroupsTreeView);


                SqlDataReader rdrSubProduct = this.GetSubProducts_Product(iCatID);
                if (rdrSubProduct.HasRows)
                {
                    while (rdrSubProduct.Read())
                    {
                        iSubCatID = Convert.ToInt32(rdrSubProduct["SubProductGroup_ID"].ToString());
                        sSubCatName = rdrSubProduct["SubProductGroupName"].ToString();

                        subProdunctNode = new TreeNode();
                        subProdunctNode.Value = rdrSubProduct["SubProductGroup_ID"].ToString();
                       subProdunctNode.Text = sSubCatName;
                       subProdunctNode.NavigateUrl = "javascript:void(0);";
                       productNode.ChildNodes.Add(subProdunctNode);


                    }
                }
                rdrSubProduct.Close();
               
            }
            

        }


        rdrProduct.Close();


    }
    public void SetTreePosition(TreeView prodGrpTree)
    {
        int yPos = 50;
        prodGrpTree.Style["Position"] = "Absolute";
        prodGrpTree.Style["Top"] = Convert.ToString(yPos) + "px";
        prodGrpTree.Style["Left"] = Convert.ToString(xPos) + "px";
        xPos = xPos + 250;

    }
    public SqlDataReader GetProducts()
    {
        SqlConnection objConnect = new SqlConnection(connectionString);

        // create new Command using stored proc name and Connection
        SqlCommand objCommand = new SqlCommand("usp_GetProductAllGroupings", objConnect);
        objCommand.CommandType = CommandType.StoredProcedure;

        try
        {
            // Open connection to the database
            objConnect.Open();

            // Execute the stored proc to initialize the DataReader
            // Connection will be closed when DataReader goes out of scope
            return objCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (Exception objErr)
        {
            throw (objErr);
        }
    }

    public SqlDataReader GetSubProducts_Product(int intProdID)
    {
        SqlConnection objConnect = new SqlConnection(connectionString);

        // create new Command using stored proc name and Connection
        SqlCommand objCommand = new SqlCommand("usp_GetSubProduct_ByProduct", objConnect);
        objCommand.CommandType = CommandType.StoredProcedure;

        SqlParameter paramSubProductGroupID = new SqlParameter("@Product_ID", SqlDbType.Int);
        objCommand.Parameters.Add(paramSubProductGroupID);
        paramSubProductGroupID.Value = intProdID;

        try
        {
            // Open connection to the database
            objConnect.Open();

            // Execute the stored proc to initialize the DataReader
            // Connection will be closed when DataReader goes out of scope
            return objCommand.ExecuteReader(CommandBehavior.CloseConnection);
        }
        catch (Exception objErr)
        {
            throw (objErr);
        }
    }

    protected void productGroupsTreeView_SelectedNodeChanged(object sender, EventArgs e)
    {
        int x = 0;


       
    }

    
       }
           
2.
ASKER CERTIFIED SOLUTION
Easwaran Paramasivam
Senior Software Engineer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 1 Comment.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 1 Comment.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros