Solved

Populate 3rd party menu control from sitemap asp.net

Posted on 2006-11-13
10
579 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

628 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