ASP.NET 2.0: How to associate asp: Menu with SqlDataSource?

I found how to associate asp: Menu with XML. I found how to associate asp: Menu with a sitemap (which really binds it to XML). How do you bind asp: Menu to the database?

The .NET Framework provides several data sources:

I want to use one that represents data from a SQL Server table. Data is stored in a standard hierarchical format , which everyone uses:

NodeID    ParentNodeID    Caption        Url
========  ==============  =========      =================
{3234...  {3632...        stackoverflow  http://stackov...
{3632...  (null)          Questions      ~/questions.aspx
{3233...  (null)          Tags           ~/tags.aspx
{3235...  {3632...        google         http://www.goo...

And the query to return all rows will be:

SELECT * FROM Nodes

What is the secret method that Microsoft intended to use to push this data into asp: Menu?


: aspalliance.com : ASP.NET 2.0. , , XML; .

+5
2

SqlDataSource, HierarchicalDataBoundControl. HierarchicalDataSourceControl. . Sitemap SiteMapDataSource, . , 3'rd , SqlDataSource.

+4

aspalliance.com: ASP.NET 2.0, .

" , , , ASP.NET 2.0. , , ".

:

protected void LoadData()
{
    DataSet ds = new DataSet();
    string connStr = YOUR_CONNECTION_STRING_HERE;
    using(SqlConnection conn = newSqlConnection(connStr))
    {
      string sql = "Select NodeID, Caption, Url, ParentID from Menu";
      SqlDataAdapter da = newSqlDataAdapter(sql, conn);
      da.Fill(ds);
      da.Dispose();
    }
    ds.DataSetName = "Menus";
    ds.Tables[0].TableName = "Menu";
    DataRelation relation = newDataRelation("ParentChild",
     ds.Tables["Menu"].Columns["NodeID"],
     ds.Tables["Menu"].Columns["ParentID"], true);

    relation.Nested = true;
    ds.Relations.Add(relation);

    xmlDataSource.Data = ds.GetXml();
}
+10