The code uses rescursive method to populate hirarchial data. I have used DNN - DotNetNuke database to demonstrate.

Here is the code:

(The code is not structured. Please forgive. You may even find hardcoded values)

public DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
SqlDataAdapter da =
new SqlDataAdapter("SELECT tabid, tabname, parentid FROM DNN_tabs WHERE isdeleted <> 1″, “your connection string”);
da.Fill(dt);

TreeView1.Nodes.Clear();
TreeNode tn = new TreeNode();
AddNodes(ref tn, “null”);
tn.Text = “Root”;
TreeView1.Nodes.Add(tn);
}


private void AddNodes(ref TreeNode tn, string parentid)
{
TreeNode ctn = new TreeNode();
string criteria = "parentid="+parentid;
if (parentid == "null") criteria = "parentid is null";
foreach (DataRow dr in dt.Select(criteria))
{
ctn = new TreeNode(dr["tabname"].ToString());
AddNodes(ref ctn, dr["tabid"].ToString());
tn.ChildNodes.Add(ctn);
}
}

In aspx file, just drag drop the treeview control from toolbox. The code will look like this:


< %@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

< !DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">





VN:F [1.1.6_502]
Rating: 0.0/5 (0 votes cast)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Related posts:

  1. Get all table names from Microsoft Access (MDB) database file
  2. List Server Varilables with ASP.NET
  3. C# ?? operator
  4. Send keystrokes to an application
  5. Javascript - Copy to Clipboard