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 DharmadasaPosted Dec 18, 2009, 3:52 AM
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.
Ruchi RPosted Dec 19, 2009, 1:42 AM
Ruchi RPosted Dec 18, 2009, 3:05 AM
Ruchi RPosted Dec 18, 2009, 1:41 AM
Nilanka DharmadasaPosted Dec 18, 2009, 12:52 AM
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 RPosted Dec 18, 2009, 12:34 AM
Nilanka DharmadasaPosted Dec 17, 2009, 6:13 AM
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 RPosted Dec 17, 2009, 1:50 AM
Srinivas KotraPosted Dec 16, 2009, 8:40 AM
Lalit MPosted Dec 16, 2009, 7:24 AM
http://www.aspnettutorials.com/tutorials/controls/treeview-node-aspnet2-csharp.aspx
http://support.microsoft.com/kb/320909