Tree view control
hi
how to populate a tree view control from database dynamically.after populating tree view i need to open or give a link to each child node(which is populated from database) to a different aspx pages
i am using c#.net and sql for this
hoping for ur help

Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 17, 2009, 10:52 PM EST
Then each child's page should have a common format.
Ex: child a >> a.aspx
child b >> b.aspx
If you can use the name of the child as the name of the page as mentioned above, then we can write the code as follows.
TreeNode cNode = new TreeNode(CRow["Dept_Name"].ToString());
cNode.NavigateUrl = string.Format("{0}.aspx",CRow["Dept_Name"].ToString());
PNode.ChildNodes.Add(cNode);
Hope this will resolve your problem.
Lalit MRank #1,186 · 1.5K reputationPosted Dec 16, 2009, 2:24 AM EST
http://www.aspnettutorials.com/tutorials/controls/treeview-node-aspnet2-csharp.aspx
http://support.microsoft.com/kb/320909
Srinivas KotraRank #8,059 · 229 reputationPosted Dec 16, 2009, 3:40 AM EST
Ruchi R91 reputationPosted Dec 16, 2009, 8:50 PM EST
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 17, 2009, 1:13 AM EST
It seems, what you have done is correct.
If you have set the following property, when you click on that node, it will direct to that page.
PNode.NavigateUrl = "Default2.aspx";
So tell me what's the problem? Please be more specific. :) I hope I can help you.
Ruchi R91 reputationPosted Dec 17, 2009, 7:34 PM EST
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 17, 2009, 7:52 PM EST
It's simple.
Change your code as follows.Focus on the lines in color blue. Other part is the code written by you.
protected void Page_Load(object sender, EventArgs e)
{
string conn = ConfigurationSettings.AppSettings["SqlConn"];
SqlConnection MyConn = new SqlConnection(conn);
SqlCommand Mycomm = new SqlCommand("SELECT tblCompany.Comp_Name, tblDepartment.Dept_Name FROM tblCompany INNER JOIN tblDepartment ON tblCompany.Comp_Id = tblDepartment.Comp_Id", MyConn);
SqlDataAdapter Dadapter = new SqlDataAdapter(Mycomm);
DataSet Ds1 = new DataSet();
Dadapter.Fill(Ds1, "tblCompany");
foreach (DataRow PRow in Ds1.Tables["tblCompany"].Rows)
{
TreeNode PNode = new TreeNode(PRow["Comp_Name"].ToString());
this.TreeView1.Nodes.Add(PNode);
PNode.NavigateUrl = "Default2.aspx";
foreach (DataRow CRow in Ds1.Tables["tblCompany"].Rows)
{
TreeNode cNode = new TreeNode(CRow["Dept_Name"].ToString());
cNode.NavigateUrl = "";//Give the url of the child here.
PNode.ChildNodes.Add(cNode);
}
PNode.Collapse();
}
Ds1.Dispose();
Dadapter = null;
MyConn.Close();
MyConn = null;
}
If you find any problem pls ask. I'm ready to help.
If you find this answer helpful, please check 'Do you like this answer' checkbox.
Ruchi R91 reputationPosted Dec 17, 2009, 8:41 PM EST
Ruchi R91 reputationPosted Dec 17, 2009, 10:05 PM EST
Ruchi R91 reputationPosted Dec 18, 2009, 8:42 PM EST