<%@ Register TagPrefix="ec" Namespace="ASPNETExpert.WebControls" Assembly="ASPNETExpert.WebControls" %>
<%@ Register TagPrefix="ecd" Namespace="ASPNETExpert.WebControls.DemoControls" Assembly="ASPNETExpert.WebControls.DemoControls" %>
<%@ Control Language="c#" Inherits="ASPNETExpert.WebControls.Demo.Tree.Programming.DataSetBuilding" CodeFile="DataSetBuilding.ascx.cs" %>
<table cellSpacing=0 cellPadding=0 border=0 xmlns:ec="urn:http://aspnetexpert.com/ExpertControls.xsd">
<tr>
<td vAlign=top>
<ec:ExpertTree id="ArticleTree" Skin="XP" runat="server" ExpandOnClick="true">
<TreeLook Width="360px" Height="380px" Overflow="Scroll" />
</ec:ExpertTree>
</td>
<td style="PADDING-LEFT: 10px" vAlign=top width="100%" rowSpan=2>
<ecd:DescriptionView runat="server" ID="Descriptionview1">
<ecd:Description id="Description1" runat="server">
Use API to build the tree at the server side from <b>DataSets</b>.
Programmatically assign properties for nodes and the tree.
</ecd:Description>
</ecd:DescriptionView>
<p></p>
</td>
</tr>
<tr>
<td><asp:label id=TreeSelection Runat="server"></asp:label></td>
</tr>
</table>
<ec:CodeViewTab id="CodeViewTab1" runat="server">
<ec:TabItem runat="server" Text="aspx" ID="Tabitem1">
<pre class="aspcode">
<ecd:SyntaxHighlight runat="server" ContentType="ASPX" OutputFile="Programming/DataSetBuilding.ascx" ID="Syntaxhighlight1"/>
</pre>
</ec:TabItem>
<ec:TabItem runat="server" Text="C#" ID="Tabitem2">
<pre class="aspcode">
<ecd:SyntaxHighlight runat="server" ContentType="C#" OutputFile="Programming/DataSetBuilding.ascx.cs" ID="Syntaxhighlight2"/>
</pre>
</ec:TabItem>
<ec:TabItem runat="server" Text="VB" ID="Tabitem3">
<pre class="aspcode">
<ecd:SyntaxHighlight runat="server" ContentType="VB" OutputFile="Programming/DataSetBuilding.ascx.vb" ID="Syntaxhighlight3"/>
</pre>
</ec:TabItem>
</ec:CodeViewTab>
namespace ASPNETExpert.WebControls.Demo.Tree.Programming
{
using System;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using ASPNETExpert.WebControls;
///
/// Summary description for DataSetBuilding.
///
public partial class DataSetBuilding : System.Web.UI.UserControl
{
protected void Page_Load(object sender, System.EventArgs e)
{
if(ArticleTree.Nodes.Count == 0)
{
BuildTree();
ArticleTree.ApplySkin();
}
}
private void BuildTree()
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
@"Data source=" + Server.MapPath("Data/articles.mdb");
try
{
conn.Open();
OleDbCommand selectCMD = new OleDbCommand("SELECT Id, ParentId, Name, Description FROM Article", conn);
OleDbDataAdapter artDA = new OleDbDataAdapter();
artDA.SelectCommand = selectCMD;
DataSet artDS = new DataSet();
artDA.Fill(artDS, "Articles");
artDS.Relations.Add("GroupRelation", artDS.Tables["Articles"].Columns["Id"], artDS.Tables["Articles"].Columns["ParentId"]);
foreach(DataRow parentRow in artDS.Tables["Articles"].Rows)
if(parentRow.IsNull("ParentId"))
BuildNode(ArticleTree.Nodes, parentRow);
}
finally
{
conn.Close();
}
}
private void BuildNode(ASPNETExpert.WebControls.TreeNodeCollection nodeColl, DataRow nodeRow)
{
ASPNETExpert.WebControls.TreeNode node = new ASPNETExpert.WebControls.TreeNode(Convert.ToInt32(nodeRow["Id"]), Convert.ToString(nodeRow["Name"]));
node.Expanded = true;
nodeColl.Add(node);
foreach(DataRow childDataRow in nodeRow.GetChildRows("GroupRelation"))
BuildNode(node.Nodes, childDataRow);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
}
#endregion
}
}