Solved

Populate 3rd party menu control from sitemap asp.net

Posted on 2006-11-13
10
570 Views
Last Modified: 2010-05-18
Programmaticlly reading sitemap file

Hi

I am using a free third party control asp.net 2.0 menu control (http://www.obout.com/tcm/tcm.aspx)  - what I would like to do is to use a web.sitemap file populate it.

I have been told that I need to adapter the code they have provided to populate the menu from a database to read the sitemap file.

How can I do this.

The database code is as follows:-


protected void Page_Load(object sender, EventArgs e)
    {
       


            // declare the menu and set its properties
            TwoColorsMenu tcm = new TwoColorsMenu();
            tcm.ID = "TwoColorsMenu1";

            // add the menu to page
            Page.Controls.Add(tcm);

            string sConnectionString;
            OleDbDataReader oReader;

            // set the connection string
            sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("DBDEMO.mdb");
            OleDbConnection Cn = new OleDbConnection(sConnectionString);

            // The database has one table called items containing both parent items and menu items
            // LEVEL shows what level the item is at (0 - parent item, 1 - belongs to menu attached to parent item, etc)
            // ORDER sets what is the item's order in the current menu (1 - first item, 2 - second item, etc.)
            // it is very important to add parent elements first, then level 1 items, then level 2 items, etc.
            string SQL = "SELECT * FROM Items ORDER BY [LEVEL], [ORDER]";
            OleDbCommand Com = new OleDbCommand(SQL, Cn);
            Cn.Open();
            oReader = Com.ExecuteReader();

            // Populate TCM.
            while (oReader.Read())
            {
                // if PARENTID is null, we're adding a parent element, otherwise it's a menu item
                tcm.Add(oReader.IsDBNull(oReader.GetOrdinal("PARENTID")) ? null : oReader.GetString(oReader.GetOrdinal("PARENTID")),
                        oReader.GetString(oReader.GetOrdinal("ID")),
                        oReader.IsDBNull(oReader.GetOrdinal("HTML")) ? "" : oReader.GetString(oReader.GetOrdinal("HTML")),
                        oReader.IsDBNull(oReader.GetOrdinal("URL")) ? null : oReader.GetString(oReader.GetOrdinal("URL")),
                        oReader.IsDBNull(oReader.GetOrdinal("URLTARGET")) ? null : oReader.GetString(oReader.GetOrdinal("URLTARGET")));
            }

            oReader.Close();
            Cn.Close();
        }





However I want to use the following test sitemap file

<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
  <siteMapNode url="~/webForms/Default.aspx" title="Home">
    <siteMapNode url="~/webForms/Default2.aspx" title="Books">
      <siteMapNode url="~/webForms/Default3.aspx" title="Novels" />
      <siteMapNode url="~/webForms/GenericErrorPage.aspx" title="History" />
      <siteMapNode url="~/webForms/Default7.aspx" title="Romance" />
    </siteMapNode>
    <siteMapNode url="~/webForms/Default8.aspx" title="Electronics" />
    <siteMapNode url="~/webForms/Default9.aspx" title="DVDs" />
    <siteMapNode url="~/webForms1/Default10.aspx" title="Computers" />
  </siteMapNode>
</siteMap>



Comments appreciated


0
Comment
Question by:EugeneC
  • 5
  • 5
10 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Try something like:

    protected void Page_Load(object sender, EventArgs e)
    {
        SiteMapNode root = SiteMap.RootNode;
        AddToMenu(root, "0");
    }

    private void AddToMenu(SiteMapNode root, string parentID)
    {
        int i = 1;
        foreach (SiteMapNode node in root.ChildNodes)
        {
            string id = string.Format("{0}.{1}", parentID, i.ToString());
            TwoColorsMenu1.Add(parentID, id, node.Title);

            if (node.HasChildNodes)
                AddToMenu(node, i.ToString());

            i++;
        }
    }
0
 

Author Comment

by:EugeneC
Comment Utility
Thanks a lot

However the menu is only being populated at the top level no sub
menu items are being populated.

I have changed the code slightly but I still cannot get it to work

Here is the code

 protected void Page_Load(object sender, EventArgs e)
    {
         
        SiteMapNode root = SiteMap.RootNode;
        AddToMenu(root, "0");  
    }

   

    private void AddToMenu(SiteMapNode root, string parentID)
    {
        int i = 1;
        foreach (SiteMapNode node in root.ChildNodes)
        {
            string id = string.Format("{0}.{1}", parentID, i.ToString());            
           
            //add top level menu item - works great
            tcm1.Add(null,id,node.Title,node.Url);

            //sub menu - these are not being added to sub menu only to top menu
            if (node.HasChildNodes)
                tcm1.Add(parentID,id,node.Title, node.Url,null);

                AddToMenu(node, i.ToString());
            i++;
        }
    }




///////////////

From the control's documentation nodes should be added like this

      // First level

      tcm1.Add(null, "id_brands", "Brands", "javascript:alert('you clicked me');void(0);", null);
      tcm1.Add(null, "id_systems", "Systems", null, null);

            // Sub menu

            tcm1.Add("id_brands", "id_ibm", "IBM", null, null);
            tcm1.Add("id_brands", "id_obout", "OBOUT", "http://www.obout.com/", null);
      
                  // Sub menu
      
                  tcm1.Add("id_obout", "id_tree", "Treeview", "treeview.aspx", null);
                  tcm1.Add("id_obout", "id_calendar", "Calendar", "calendar.aspx", null);


Any pointed would be welcomed

Thanks
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
Comment Utility
I think it was almost there first time round. I've made a couple of minor tweaks and it now works ok for me:

    protected void Page_Load(object sender, EventArgs e)
    {
        SiteMapNode root = SiteMap.RootNode;
        AddToMenu(root, null);
    }

    private void AddToMenu(SiteMapNode root, string parentID)
    {
        int i = 1;
        foreach (SiteMapNode node in root.ChildNodes)
        {
            string id = string.Format("{0}.{1}", parentID, i.ToString());

            // Add the current node
            tcm1.Add(parentID, id, node.Title, node.Url);

            // Process current nodes children
            if (node.HasChildNodes)
                AddToMenu(node, id);

            i++;
        }
0
 

Author Comment

by:EugeneC
Comment Utility
Thanks a lot

Sorry to sound thick but does is work with the menu control?
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
I'm not sure without trying it, but I would have thought it should. It may need tweaking a little but the basic looping structure of the code should work still.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:EugeneC
Comment Utility
I have tried but it just does not populate?  
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
Your easiest option would be to add a SiteMapDataSource object to the page and use that as the DataSource for the Menu control. That way you don't need to manually code it.
0
 

Author Comment

by:EugeneC
Comment Utility
Thanks a lot carl but I need to use this control and have to do it programmatically
0
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
In that case try:

    protected void Page_Load(object sender, EventArgs e)
    {
        MenuItem root = new MenuItem(SiteMap.RootNode.Title, SiteMap.RootNode.Description, null, SiteMap.RootNode.Url);
        Menu1.Items.Add(root);

        AddToMenu(SiteMap.RootNode, root);
    }

    private void AddToMenu(SiteMapNode parent, MenuItem root)
    {
        foreach (SiteMapNode node in parent.ChildNodes)
        {
            MenuItem newItem = new MenuItem(node.Title, node.Description, null, node.Url);
            root.ChildItems.Add(newItem);

            if (node.HasChildNodes)
                AddToMenu(node, newItem);
        }
    }
0
 

Author Comment

by:EugeneC
Comment Utility
Dohh!  Yep it works now Carl - thank a lot for your help
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now