CodeVerge.Net Beta
Login Idy
Register Password
  Forgot?
Explore    Item Entry    Members   
NEWSGROUP
.NET
Algorithms-Data Structures
Asp.Net
C Plus Plus
CSharp
Database
HTML
Javascript
Linq
Other
Regular Expressions
VB.Net
XML





Zone: > NEWSGROUP > Asp.Net Forum > general_asp.net.master_pages_themes_and_navigation_controls Tags:
Item Type: NewsGroup Date Entered: 1/29/2008 4:17:41 PM Date Modified: Subscribers: 0 Subscribe Alert
Rate It:
(NR, 0)
XPoints: N/A Replies: 1 Views: 8 Favorited: 0 Favorite
Can Reply:  No Members Can Edit: No Online: Yes
2 Items, 1 Pages 1 |< << Go >> >|
soha0
Asp.Net User
Populating a .Net Treeview control from a parent-child relationship db table1/29/2008 4:17:41 PM

0/0

I want populating a C#.Net treeview control in a Asp page with records from a database table containing a parent-child relationship.
example for db:



RecordID

(autonumber)
ParentID DisplayName
1 0 Topic 1
2 0 Topic 2
3 1 RE: Topic 1
4 1 RE: Topic 1
5 2 RE: RE: Topic 1
6 2 RE: RE: Topic 1
Amanda Wang - M
Asp.Net User
Re: Populating a .Net Treeview control from a parent-child relationship db table1/31/2008 8:56:01 AM

0/0

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
2 Items, 1 Pages 1 |< << Go >> >|



Search This Site:


Meet Our Sponsors:



Other Resources:

TPageControl : Page by name - borland.public.delphi.language.delphi ... TPageControl : Page by name, > ROOT > NEWSGROUP > Borland Forums > borland.public.delphi.language.delphi.general, Date: ... parent-child relationship db table ...
Populating a .Net Treeview control from a parent-child relationship db ... We will be populating a VB.Net treeview control in a Windows form with records from a database table containing a ... a parent-child relationship db table. ...
Populating a .Net Datagrid Control from a TreeNode Selection ... Populating a .Net Treeview control from a parent-child relationship db table. ... Populating a .Net Treeview control from a parent-child relationship db ...
CodeProject: Populating DataGrid control from a parent-child ... ... to populate a DataGrid with records from a database table containing a parent-child relationship.; Author: Jimmy M ... parent-child relationship DB table. By ...
Populating a treeview recursively - MSDN Forums ... trying to figure out poplulating a winform Tree control with a self join table. ... table (NODE_DESCR) to other rows in a parent/child relationship *recursively ...
Display Hierarchical Data with TreeView in ASP.NET 2.0: ASP Alliance ... demonstrates how to utilize the new TreeView control in ASP.NET 2.0 to display hierarchical ... parent node and sc is used to reference the parent table from ...


 
All Times Are GMT