Controlling ASP.NET Menus Programmatically (how to programatically navigate from a menu?)

Last post 08-23-2007 12:44 PM by jleviaguirre. 5 replies.

Sort Posts:

  • Controlling ASP.NET Menus Programmatically (how to programatically navigate from a menu?)

    07-24-2007, 11:58 PM
    • Member
      23 point Member
    • jleviaguirre
    • Member since 04-17-2007, 2:24 PM
    • Posts 82

    This nice article explains how to synchronize two menus. In my case i put one horizontal and the other vertical but I am stuck in a very simple task (I am a beginner) 

     how do I make it to navigate to the mapped page when clicking the second menu? I realize that that is the way is supposed to work because at the end of the article it says that it catches the click event instead of navigating to any location:

    The preceding code catches the click event of Menu1. Instead of navigating to any location, you use the value to determine what the second Menu control displays. You accomplish that by adjusting the StartingNodeUrl property of the second Menu control's SiteMapDataSource control.

    I tweaked a little bit the code but don't know if that's the cause.

    I want to either navigate to an aspx page or load a control into the content placeholder in Default.aspx when clicking an item from the second menu.

     

    Please help Ick! 

     

     

    Here are the files:

    Web.sitemap

    1    <?xml version="1.0" encoding="utf-8" ?>
    2    <siteMap>
    3      <siteMapNode title="ROOT">
    4        <siteMapNode title="Browse Projects">
    5          <siteMapNode title="Search" url="searchProjects.aspx"/>
    6          <siteMapNode title="View Workflows" url="viewWorkflows.aspx"/>
    7          <siteMapNode title="View Projects" url="viewProjects.aspx"/>
    8          <siteMapNode title="View Teams" url="viewTeams.aspx"/>
    9        </siteMapNode>
    10       <siteMapNode title="Manage Projects">
    11         <siteMapNode title="Enter new Project" url="Pages/newProject.aspx"/>
    12         <siteMapNode title="Enter new Workflow" url="Pages/newWorkflow.aspx"/>
    13       </siteMapNode>
    14       <siteMapNode title="My GKD">
    15         <siteMapNode title="My Projects" url="myProjects.aspx"/>
    16         <siteMapNode title="Edit Profile" url="myProfile.aspx"/>
    17       </siteMapNode>
    18     </siteMapNode>
    19   </siteMap>
     
     MasterPage.master.cs 
    1    using System;
    2    using System.Data;
    3    using System.Configuration;
    4    using System.Collections;
    5    using System.Web;
    6    using System.Web.Security;
    7    using System.Web.UI;
    8    using System.Web.UI.WebControls;
    9    using System.Web.UI.WebControls.WebParts;
    10   using System.Web.UI.HtmlControls;
    11   
    12   public partial class MasterPage : System.Web.UI.MasterPage {
    13       protected void Menu_Top_MenuItemClick(object sender, MenuEventArgs e) {
    14           {
    15               switch (e.Item.Value) {
    16                   case "Browse Projects":
    17                       SiteMapDataSourceGKD2.StartingNodeUrl = "searchProjects.aspx";
    18                       return;
    19                   case "Manage Projects":
    20                       SiteMapDataSourceGKD2.StartingNodeUrl = "Pages/newProject.aspx";
    21                       return;
    22                   case "My GKD":
    23                       SiteMapDataSourceGKD2.StartingNodeUrl = "myProjects.aspx";
    24                       return;
    25               }
    26           }
    27       }
    28       protected void Menu_Left_MenuItemClick(object sender, MenuEventArgs e) {
    29           MenuItem m = Menu_Left.SelectedItem;
    30           string tmp = "&lt;li>" + m.Target +
    31               "&lt;li>" + m.NavigateUrl +
    32               "&lt;li>" + m.Value +
    33               "&lt;li>" + m.parent +
    34               "&lt;li>" + m.Value +
    35               "&lt;li>" + m.ValuePath;/* +
    36               "<li>" + m.Parent.NavigateUrl +
    37               "<li>" + m.Parent.Target +
    38               "<li>" + m.Parent.Value;
    39           */
    40           contentPlaceHolder.Controls.Add(new LiteralControl(tmp));
    41   
    42           //Response.Write(tmp);
    43           //Response.Redirect();
    44       }
    45   }
    
     

    MasterPage.master

    1    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
    2    
    3    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    4    
    5    <html xmlns="http://www.w3.org/1999/xhtml" >
    6    <head runat="server">
    7        <title>GKD Geochemical Knowledge Desktop</title>
    8        <link href="css_gks/Master.css" rel="stylesheet" type="text/css" />
    9    </head>
    10   <body style="margin:0;height:100%">
    11       <form id="form1" runat="server">
    12           <table id="masterViewPort" cellpadding="0";cellspacing="0" border="0">
    13               <tr>
    14                   <td colspan="3" style="background-image: url(img_gks/banner_back.gif); height: 20px; color:#fff; font-weight: bold; font-size: small; width: 100%; text-align: right;">
    15                       Geochemical Knowledge Desktop Ver 2007.07.23 [Development]
    16                   </td>
    17               </tr>
    18               <tr>
    19                   <td style="height:100%; vertical-align: top; background-color: #e7efff; border-right: #d7e1f6 1px solid; text-align: center;">
    20                       <div style="width: 100%; height: 77px; background-color: #336699; text-align: center; font-size: x-small; vertical-align: text-bottom; color: white;">
    21                           <img align="top" src="img_gks/ou_logo_global40.gif" style="margin: 5px;" /><br />
    22                           GKD Guest User
    23                           <br />
    24                           @ houic-s-604</div>
    25                         
    26                       <asp:Menu ID="Menu_Left" runat="server" CssClass="topMenu" Height="40px" ItemWrap="True" StaticEnableDefaultPopOutImage="False" DataSourceID="SiteMapDataSourceGKD2" Width="100%" MaximumDynamicDisplayLevels="0" OnMenuItemClick="Menu_Left_MenuItemClick">
    27                           <StaticMenuItemStyle BorderStyle="Outset" BorderWidth="1px" Height="40px" BackColor="#D7E1F6" HorizontalPadding="10px" />
    28                           <StaticSelectedStyle BackColor="#336699" BorderStyle="Inset" Font-Bold="True" ForeColor="White" BorderWidth="1px" />
    29                           <DataBindings>
    30                               <asp:MenuItemBinding DataMember="SiteMapNode" TextField="Title" />
    31                           </DataBindings>
    32                           <Items>
    33                               <asp:MenuItem Text="My GKD" Value="My GKD"></asp:MenuItem>
    34                               <asp:MenuItem Text="Administration" Value="Administration"></asp:MenuItem>
    35                               <asp:MenuItem Text="Browse Database" Value="Browse Database"></asp:MenuItem>
    36                               <asp:MenuItem Text="Logout" Value="Logout"></asp:MenuItem>
    37                           </Items>
    38                           <StaticHoverStyle BackColor="#E0E0E0" BorderStyle="Inset" Height="40px" ForeColor="Black" />
    39                       </asp:Menu>
    40                   </td>
    41                   <td colspan="2" style="vertical-align:top; width: 100%;">
    42   <div style="background-color: #e7efff; border-bottom: #d7e1f6 1px solid;">
    43   <img src="img_gks/tmenu_square_fade.gif" align="right" /><asp:Menu ID="Menu_Top" runat="server" CssClass="topMenu" ItemWrap="True" StaticEnableDefaultPopOutImage="False" DataSourceID="SiteMapDataSourceGKD" MaximumDynamicDisplayLevels="0" OnMenuItemClick="Menu_Top_MenuItemClick" Orientation="Horizontal">
    44       <StaticMenuItemStyle BorderStyle="Outset" BorderWidth="1px" Height="40px" BackColor="#D7E1F6" HorizontalPadding="10px" />
    45       <StaticSelectedStyle BackColor="#336699" BorderStyle="Inset" Font-Bold="True" ForeColor="White" BorderWidth="1px" />
    46       <DataBindings>
    47           <asp:MenuItemBinding DataMember="SiteMapNode" TextField="Title" />
    48       </DataBindings>
    49       <Items>
    50           <asp:MenuItem Text="My GKD" Value="My GKD"></asp:MenuItem>
    51           <asp:MenuItem Text="Administration" Value="Administration"></asp:MenuItem>
    52           <asp:MenuItem Text="Browse Database" Value="Browse Database"></asp:MenuItem>
    53           <asp:MenuItem Text="Logout" Value="Logout"></asp:MenuItem>
    54       </Items>
    55       <StaticHoverStyle BackColor="#E0E0E0" BorderStyle="Inset" Height="40px" ForeColor="Black" />
    56   </asp:Menu>
    57   </div>
    58                       <asp:ContentPlaceHolder runat="server" ID="contentPlaceHolder">
    59                           </asp:ContentPlaceHolder></td>
    60               </tr>
    61           </table>
    62           <asp:SiteMapDataSource ID="SiteMapDataSourceGKD" runat="server" ShowStartingNode="False" />
    63           <asp:SiteMapDataSource ID="SiteMapDataSourceGKD2" runat="server" ShowStartingNode="False"
    64               StartingNodeOffset="-1" StartingNodeUrl="~/Default.aspx" />
    65       </form>
    66   </body>
    67   </html>
    68   
    

     

    Default.aspx

    1 <%@ Page MasterPageFile="~/MasterPage.master" Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    2 hello world
    
    
     
  • Re: Controlling ASP.NET Menus Programmatically (how to programatically navigate from a menu?)

    07-26-2007, 11:56 PM

    Hi,

    I think there is a litte difference between your requeirment and this article content.

    I this article: It talks how to program the ASP.NET Menu control and create two menus bound to the same Web.sitemap file that work together, and the content of the second menu is determined by which menu item is selected in the first menu.

    But, as your description,  your second menu's content is not determined by the first menu, so i think you don't need  to refer to the article completely.

    You can make your menus bound to the same Web.sitemap file together, the codebehind of the menuitemclick is not necessary.

    Below is a simple:

    <asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1" MaximumDynamicDisplayLevels="0"
                OnMenuItemClick="Menu1_MenuItemClick" Orientation="Horizontal">
                <DataBindings>
                    <asp:MenuItemBinding DataMember="SiteMapNode" TextField="Title" />
                </DataBindings>
            </asp:Menu>
        
        </div>
            <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />
            <asp:Menu ID="Menu2" runat="server" DataSourceID="SiteMapDataSource2" Orientation="Horizontal">
            </asp:Menu>
            <asp:SiteMapDataSource ID="SiteMapDataSource2" runat="server" ShowStartingNode="False"
                StartingNodeOffset="-1" />

    hope it helps.

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Controlling ASP.NET Menus Programmatically (how to programatically navigate from a menu?)

    07-27-2007, 6:02 PM
    • Member
      23 point Member
    • jleviaguirre
    • Member since 04-17-2007, 2:24 PM
    • Posts 82

    Hi Amanda,

     My second menu is dependent on the first one. That's why I follow the article. What I can't figure out is how to set and retreive the values for the second menu.

     Right now I am in a point where I modified the web.sitemap because I want to dynamically load a control when the user clicks on the second menu. For that I coded the page_load event in myPage.aspx:

      

    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ContentPlaceHolder cph = this.Master.FindControl("ContentPlaceHolderMain") as ContentPlaceHolder;
            SiteMapDataSource smds = this.Master.FindControl("SiteMapDataSourceGKD2") as SiteMapDataSource;
            Menu m = this.Master.FindControl("Menu_left") as Menu;
    
            string ctrlUrl = m.SelectedValue;
            cph.Controls.Add(new LiteralControl(ctrlUrl));
            //UserControl uc = this.LoadControl(smds.currentNode) as UserControl;
            //cph.Controls.Add(uc);
        }
            
     }
    

    It works fine if I uncomment last two lines and hardcode the loadControl parameter but I don't know how to get the m.selectedItem.url or even the SiteMap.CurrentNode.Url because it's null. Is there a way to synchronize the sitemap and teh menu so I can get the url, value, tooltip or even description from the web.sitemap or m.selectedItem?

  • Re: Controlling ASP.NET Menus Programmatically (how to programatically navigate from a menu?)

    07-29-2007, 11:23 PM

    Hi,

    I think you need you to make your second menu and the SiteMapPath point to the same SiteMap File, so you can config them in the Web.config:

    for Example:

    1. Web.config (config the SiteMap's providers):

       <siteMap>
          
    <providers>
            
    <add siteMapFile="~/App_Data/Job.sitemap" name="JobXmlSiteMapProvider"
                type
    ="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
          
    </providers>
        
    </siteMap>
    2. aspx file (specify the SiteMapProvoder for the SiteMapPath):
    <asp:SiteMapPath ID="jobSiteMapPath" runat="server" PathSeparator=" → " SiteMapProvider="JobXmlSiteMapProvider"></asp:SiteMapPath>

     3. cs code (The second menu's datasource is SitemapDataSource, you can specify the SiteMapProvoder for the second menu):

     this.SiteMapDataSource1.Provider =  SiteMap.Providers["JobXmlSiteMapProvider"];
    Hope it can help you!
    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: Controlling ASP.NET Menus Programmatically (how to programatically navigate from a menu?)

    07-30-2007, 4:30 PM
    Answer
    • Member
      23 point Member
    • jleviaguirre
    • Member since 04-17-2007, 2:24 PM
    • Posts 82

    Found the problem!

    I did not follow EXACTLY the instructions on the article. I just did a copy +  paste from the first menu to the other and forgot to remove the databindings tag in the second menu. In other words, i removed the following lines and it worked:

    29                           <DataBindings>
    30                               <asp:MenuItemBinding DataMember="SiteMapNode" TextField="Title" />
    31                           </DataBindings>

     Your help was helpful anyways. Thanks a lot.

     J

  • Re: Controlling ASP.NET Menus Programmatically (how to programatically navigate from a menu?)

    08-23-2007, 12:44 PM
    • Member
      23 point Member
    • jleviaguirre
    • Member since 04-17-2007, 2:24 PM
    • Posts 82

    Check out this post http://forums.asp.net/p/1143593/1873682.aspx#1873682 dated same as this one. It is a simple solution for the original problem without almost any code behind.

Page 1 of 1 (6 items)