Populate 3rd party menu control from sitemap asp.net

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


EugeneCAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
EugeneCAuthor Commented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
EugeneCAuthor Commented:
Thanks a lot

Sorry to sound thick but does is work with the menu control?
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
EugeneCAuthor Commented:
I have tried but it just does not populate?  
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
EugeneCAuthor Commented:
Thanks a lot carl but I need to use this control and have to do it programmatically
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
EugeneCAuthor Commented:
Dohh!  Yep it works now Carl - thank a lot for your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.