Hi
You want to pop a treecview from the database, you can refer this link: http://aspalliance.com/822
It talks how to generate a menu control from the database, but you can refer it.
Below is my test code to generate a treeview refer the above link,
1.page's aspx code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Treeview_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server">
<DataBindings>
<asp:TreeNodeBinding DataMember="MenuItem"
NavigateUrlField="NavigateUrl" TextField="Text" ToolTipField="ToolTip"/>
</DataBindings>
</asp:TreeView>
</div>
<asp:XmlDataSource ID="xmlDataSource" TransformFile="~/TransformXSLT.xsl"
XPath="MenuItems/MenuItem" runat="server" EnableCaching="False" OnDataBinding="xmlDataSource_DataBinding"/>
</form>
</body>
</html>
2. the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
popTreeView();
}
}
protected void popTreeView()
{
DataSet ds = new DataSet();
string connStr = "Data Source=AMANDA-WANG717\\SQLEXPRESS;Initial Catalog=MenuDB;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connStr))
{
string sql = "Select MenuID, Text, Description, ParentID,[Role] from Menu";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds);
da.Dispose();
}
ds.DataSetName = "Menus";
ds.Tables[0].TableName = "Menu";
DataRelation relation = new DataRelation("ParentChild", ds.Tables["Menu"].Columns["MenuID"], ds.Tables["Menu"].Columns["ParentID"], true);
relation.Nested = true;
ds.Relations.Add(relation);
xmlDataSource.Data = ds.GetXml();
this.TreeView1.DataSource = xmlDataSource;
this.TreeView1.DataBind();
if (Request.Params["Sel"] != null)
Page.Controls.Add(new System.Web.UI.LiteralControl("You selected " + Request.Params["Sel"]));
}
protected void xmlDataSource_DataBinding(object sender, EventArgs e)
{
}
Hope it helps.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Yours sincerely,
Amanda Wang
Microsoft Online Community Support