Avatar of Saroj13
Saroj13
 asked on

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

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;


       
    }

    
       }

Open in new window

           
2.
ASP.NETC#

Avatar of undefined
Last Comment
Easwaran Paramasivam

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Easwaran Paramasivam

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23