Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am stuck with one issue. Need to display employee details when user selects particular employee in treeview control.

Binding data from Sqldatasource through store procedure. Is it possible to display employee details in treeview control.

Thanks
Posted

As your question you need Event handler for TreeView

Add this to your treeview control in .aspx file:

ASP
OnSelectedNodeChanged="yourTreeView_SelectedNodeChanged"


And add this function into your code behind:

C#
protected void yourTreeView_SelectedNodeChanged(object sender, EventArgs e)
{
    //do your SQL operation here
}
 
Share this answer
 
Thank You Tiririk for you suggestion...


If you dont mind can you post some code for selectednodechange which can be useful for me..

Below is my code once check and let me know...

C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null)
        {
            Session["Msg"] = "Your session has expired. please login again";
            Response.Redirect("../Account/login.aspx");
            return;
        }
       
        if (!IsPostBack)
        {
            GetTreeViewItems();
        }
    }

           

    private void GetTreeViewItems()
    {
        try
        {
           string cs = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;
            SqlConnection con = new SqlConnection(cs);
            SqlDataAdapter da = new SqlDataAdapter("sp_GetEmployee", con);    
            DataSet ds = new DataSet();
            da.Fill(ds);

            ds.Relations.Add("ChildRows", ds.Tables[0].Columns["id"], ds.Tables[0].Columns["ManagerId"]);
            foreach (DataRow level1DataRow in ds.Tables[0].Rows)
            {
                if (string.IsNullOrEmpty(level1DataRow["ManagerId"].ToString()))
                {
                    TreeNode parentTreeNode = new TreeNode();
                    parentTreeNode.Text = level1DataRow["FirstName"].ToString();
                  // parentTreeNode.Text = level1DataRow["Designation"].ToString();
                    parentTreeNode.Value = level1DataRow["id"].ToString();

                    GetChildRows(level1DataRow, parentTreeNode);
                    TreeView1.Nodes.Add(parentTreeNode);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void GetChildRows(DataRow dataRow, TreeNode treeNode)
    {
        DataRow[] childRows = dataRow.GetChildRows("ChildRows");
        foreach (DataRow childRow in childRows)
        {
            TreeNode childTreeNode = new TreeNode();
            childTreeNode.Text = childRow["FirstName"].ToString();
          // childTreeNode.Text = childRow["Designation"].ToString();
            childTreeNode.Value = childRow["id"].ToString();
            treeNode.ChildNodes.Add(childTreeNode);
            if (childRow.GetChildRows("ChildRows").Length > 0)
            {
                GetChildRows(childRow, childTreeNode);
            }
        }
    }
    protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
    {
        string Prefix = "<div style="display:none;" class="çontent'">Employee</div>";
        e.Node.Text = Prefix + e.Node.Text;
    }


    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900