master page with two menus, content place holder, sitemap and dynamic controls

Last post 08-08-2008 7:41 AM by hurzwurz. 7 replies.

Sort Posts:

  • master page with two menus, content place holder, sitemap and dynamic controls

    08-07-2007, 3:52 PM
    • Member
      23 point Member
    • jleviaguirre
    • Member since 04-17-2007, 6:24 PM
    • Posts 82

    Dear all,

    I have a master page with two menus. In fact, I want both menus to remain selected after I pick an item from the second menu.

    When I click the first menu, the second menu populates from my web.sitemap but when I click the second menu, the pages "reloads" and looses the selections in both menus. If i click the first menu item again, then the page works as intended.

    How can I programatically click the selected item from the top menu? Is that the right solution? I am planning to add some controls into the contentplaceholder dynamically driven from the second menu.


    If you run my code, you will understand what I am talking about. 

    I posted all my code here

    the relevant lines of code are here: 

    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>Untitled Page</title>
    8        <link href="css_gks/Master.css" rel="stylesheet" type="text/css" />
    9    </head>
    10   <body>
    11       <form id="form1" runat="server">
    12           <table border="0" cellpadding="0" cellspacing="0" style="width: 100%; height: 100%">
    13               <tr>
    14                   <td colspan="2" style="height: 1px">
    15                       <asp:Menu ID="Menu1" runat="server" CssClass="topMenu" DataSourceID="SiteMapDataSource1"
    16                           MaximumDynamicDisplayLevels="0" OnMenuItemClick="Menu1_MenuItemClick" Orientation="Horizontal">
    17                           <DataBindings>
    18                               <asp:MenuItemBinding DataMember="SiteMapNode" TextField="Title" />
    19                           </DataBindings>
    20                           <StaticMenuItemStyle BackColor="#D7E1F6" BorderStyle="Outset" CssClass="menuItem" />
    21                           <StaticSelectedStyle BackColor="#336699" BorderStyle="Inset" CssClass="menuItem"
    22                               ForeColor="White" />
    23                           <StaticHoverStyle BackColor="#E0E0E0" BorderStyle="Inset" CssClass="menuItem" />
    24                       </asp:Menu>
    25                   </td>
    26               </tr>
    27               <tr>
    28                   <td style="vertical-align: top; width: 1px;">
    29                       <asp:Menu ID="Menu2" runat="server" DataSourceID="SiteMapDataSource2">
    30                           <StaticMenuItemStyle BackColor="#D7E1F6" BorderStyle="Outset" CssClass="menuItem" />
    31                           <StaticSelectedStyle BackColor="#336699" BorderStyle="Inset" CssClass="menuItem"
    32                               ForeColor="White" />
    33                           <StaticHoverStyle BackColor="#E0E0E0" BorderStyle="Inset" CssClass="menuItem" />
    34                       </asp:Menu>
    35                   </td>
    36                   <td valign="top">
    37           <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
    38           </asp:contentplaceholder>
    39                   </td>
    40               </tr>
    41           </table>
    42           <asp:SiteMapDataSource ID="SiteMapDataSource2" runat="server" ShowStartingNode="False"
    43               StartingNodeOffset="-1" StartingNodeUrl="gkd.aspx?ctx=srch" />
    44           <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />
    45       </form>
    46   </body>
    47   </html>
    48   
    
       
    1        protected void Menu1_MenuItemClick(object sender, MenuEventArgs e) {
    2            switch (e.Item.Value) {
    3                case "Browse Projects":
    4                    SiteMapDataSource2.StartingNodeUrl = "gkd.aspx?ctx=srch";
    5                    break;
    6                case "Manage Projects":
    7                    SiteMapDataSource2.StartingNodeUrl = "gkd.aspx?ctx=npj";
    8                    break;
    9                case "My GKD":
    10                   SiteMapDataSource2.StartingNodeUrl = "gkd.aspx?ctx=mypjs";
    11                   break;
    12           }
    13   
    
     
  • Re: master page with two menus, content place holder, sitemap and dynamic controls

    08-09-2007, 11:43 AM
    • Member
      215 point Member
    • Android
    • Member since 05-10-2005, 7:28 AM
    • Posts 68

    Did you manage to solve this one? If not, I have a potential solution for you. You could use a MultiView control to give the impression that separate sub-pages are being opened. Not ideal, I know, but at least it leaves the top level selection active.

  • Re: master page with two menus, content place holder, sitemap and dynamic controls

    08-10-2007, 12:28 PM
    • Member
      23 point Member
    • jleviaguirre
    • Member since 04-17-2007, 6:24 PM
    • Posts 82

    No, I was not able to. I had to remove the databindings from the menus. Only the last menu item remains selected. I guess the solution is to add extra code to climb up the selected item all the way to the root and select it (apply the selected style)

     Another approach is to do it via javascript but I will try the first aproach.
     

  • Re: master page with two menus, content place holder, sitemap and dynamic controls

    08-13-2007, 12:31 AM
    Answer

    Hi,

    You can try to use microsoft's CSS Menu Adapter of the ASP.NET 2.0 CSS Friendly Control Adapters collection, it is much more flexible then the Standar menu control.

     

    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: master page with two menus, content place holder, sitemap and dynamic controls

    08-13-2007, 5:50 AM
    • Member
      215 point Member
    • Android
    • Member since 05-10-2005, 7:28 AM
    • Posts 68

    I'm already using that, but thanks for the suggestion. I may have to abandon the Menu control and SiteMap. Instead, I think I may have to hard-wire my navigation buttons. Not as flexible, but OK if my site structure remains stable (which it will).

  • Re: master page with two menus, content place holder, sitemap and dynamic controls

    08-16-2007, 6:17 AM
    • Member
      215 point Member
    • Android
    • Member since 05-10-2005, 7:28 AM
    • Posts 68

    I don't know if you're still struggling with this, but I recently came across this post, which has solved the problem for me.

  • Re: master page with two menus, content place holder, sitemap and dynamic controls

    08-23-2007, 12:37 PM
    Answer
    • Member
      23 point Member
    • jleviaguirre
    • Member since 04-17-2007, 6:24 PM
    • Posts 82

    I figure out a simpler way!

    The concept is as follows:

    1. When the user clicks on the first menu, save the Menu1.SelectedItem reference into a session or something.
    2. When the user clicks on the second menu, set the saved reference selected property to true

    but for some reasons I was not able to save an object reference so I had to tweak the approach:

    1. When the user clicks on the first menu, save the Menu1.SelectedItem.DataPath (or the index count of the selected item) into a session.  or something such as lastTopMenuItemDataPath
    2. When the user clicks on the second menu, select the menu item from the first menu that matches the first menu. 

    Having said the solution, here is the code:

    For the master page:
     

    <asp:Menu ID="Menu2" runat="server" DataSourceID="SiteMapDataSource2" MaximumDynamicDisplayLevels="0">
        <StaticMenuItemStyle CssClass="menuItem" />
        <StaticSelectedStyle CssClass="menuItemSelected" />
        <StaticHoverStyle CssClass="menuItemHover" />
    </asp:Menu>
    <asp:SiteMapDataSource ID="SiteMapDataSource2" runat="server" ShowStartingNode="false" StartingNodeOffset="1" />
    
    <asp:Menu ID="Menu1" OnPreRender="setRef" runat="server" DataSourceID="SiteMapDataSource1" MaximumDynamicDisplayLevels="0" Orientation="Horizontal">
        <StaticMenuItemStyle CssClass="menuItem" />
        <StaticSelectedStyle CssClass="menuItemSelected" />
        <StaticHoverStyle CssClass="menuItemHover" />
    </asp:Menu>
    <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false"  />
    
    <asp:ContentPlaceHolder OnPreRender="getRef" ID="ContentPlaceHolder1" runat="server">
    </asp:ContentPlaceHolder>
      

    The master page code behind:

     

    public partial class MasterPage : System.Web.UI.MasterPage {
    
        protected void setRef(object sender, EventArgs e) {
            if (Menu1.SelectedItem != null) Session["lastTopMenuItemDataPath"] = Menu1.SelectedItem.DataPath;
        }
        protected void getRef(object sender, EventArgs e) {
            string ldp = Session["lastTopMenuItemDataPath"].ToString();
            if(ldp !=null) foreach (MenuItem m in Menu1.Items) m.Selected = (m.DataPath == ldp);
        }
    }
     
  • Re: master page with two menus, content place holder, sitemap and dynamic controls

    08-08-2008, 7:41 AM
    • Member
      27 point Member
    • hurzwurz
    • Member since 10-10-2007, 8:06 AM
    • Posts 81

     hey

    I also have two menus, to datasources, 1 web.sitemap

    and I tried your trick.it works

    but I have some other problems with menu.

    maybe you can give me some advice..

     

    If I click on the first (horizontal menu) menu, than the menu on the left shows the sitemap nodes.

    but the content page,does not update/shows the content of the page clicked in the first tab


    also If I chose 1 page of the 2nd menu (left), the menu itself, does not longer display the subnote from the first menu

    it displays the subnotes/menus which are set in datasource2 to starting node

    ( I programmatically change the startingnode of the second menu, like found in a post the forum

    using cases in the master.cs )

     may you have any idea how to keep the right subnotes/menus displayed?

    greetings

     

     

    solved my problem

     

    http://forums.asp.net/t/1302691.aspx

     

     

     

     

     

     

Page 1 of 1 (8 items)