Solved

Populate 3rd party menu control from sitemap asp.net

Posted on 2006-11-13
10
573 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
ID: 17929607
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
ID: 17930052
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
ID: 17930185
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:EugeneC
ID: 17930375
Thanks a lot

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

Expert Comment

by:Carl Tawn
ID: 17930389
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
 

Author Comment

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

Expert Comment

by:Carl Tawn
ID: 17930443
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
ID: 17930653
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
ID: 17930755
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
ID: 17931216
Dohh!  Yep it works now Carl - thank a lot for your help
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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