MasterPages - Dynamically Created Menus Disappearing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • LvBohemian

    MasterPages - Dynamically Created Menus Disappearing

    I am playing around with creating some menus
    dynamically, and they
    create fine and show up ok when I want them to; but
    when I select a
    menu NavigateUrl at run time, the applicable url shows
    up as
    anticipated in the content place holder but the menu
    disappears...

    the menu is part of the master page and is not in the
    content place holder...

    anybody have any ideas?

    am I missing something obvious?

    when I create the same menu at design time it does not
    exhibit this behavior...

    thanks in advance.

  • Mark Rae

    #2
    Re: MasterPages - Dynamically Created Menus Disappearing

    "LvBohemian " <LvBohemian@dis cussions.micros oft.comwrote in message
    news:29B06806-7E6E-4F6D-AA08-1D8AA0220147@mi crosoft.com...
    the menu is part of the master page and is not in the
    content place holder...
    That's good, but where is the code which does the dynamic generation of the
    MenuItems collection...?


    Comment

    • LvBohemian

      #3
      Re: MasterPages - Dynamically Created Menus Disappearing

      This is how I am binding, with the exception that I am calling
      CreateMenuItemB inding from within the click event of a button for test
      purposes...



      As I said everything works fine, all menu items show up as anticipated, then
      when I select a menu item which loads the applicable NavigateUrl into the
      MasterPage ContentPlaceHol der I lose the menu, it disappears, although the
      NavagateUrl *.aspx appears in the ContentPlaceHol der as it should...

      If I can get the menu to stick around after I select the NavigateUrl from
      the menu I created dynamically and display the NavigateUrl in the MasterPage
      ContentPlaceHol der, then I can move forward with my idea that I am kicking
      around...

      Thanks in advance.

      Comment

      • Mark Rae

        #4
        Re: MasterPages - Dynamically Created Menus Disappearing

        "LvBohemian " <LvBohemian@dis cussions.micros oft.comwrote in message
        news:4A3E843A-6760-49EC-BAD4-B50ABB959760@mi crosoft.com...
        when I select a menu item which loads the applicable NavigateUrl into the
        MasterPage ContentPlaceHol der
        I'm not sure what you're doing here...

        Generally speaking, in the MasterPage / ContentPage architecture, you do not
        dynamically select the ContentPage through a menu - instead, you simply
        navigate to another ContentPage which, when it loads, picks up its
        MasterPage and merges the two files into one HTML document...

        E.g. if you have Page1.aspx and Page2.aspx, both of which are ContentPages
        and use the same MasterPage, your MenuItem should simply navigate to
        Page2.aspx through its NavigateUrl property - it should certainly not be
        trying to play about with the contents of the ContentPlaceHol der on the
        MasterPage...


        Comment

        • LvBohemian

          #5
          Re: MasterPages - Dynamically Created Menus Disappearing

          I hope this explains it better...

          The master page that I am dynamically creating a menu on works fine...

          I am trying to select the content page that I want to display on the same
          master page; from the dynamic menu I created, much as you would do from a
          TreeView control as is done on the MSDN page, it works as anticipated if I
          create a static menu at design time; but using the same settings at runtime,
          when I select a menu item it does display the content page as it is supposed
          to but the menu disappears, even though it is part of the master page...
          I want the menu to stick around in case I want to load a different content
          page instead in the same MasterPage…

          Thanks in advance.

          Comment

          • Mark Rae

            #6
            Re: MasterPages - Dynamically Created Menus Disappearing

            "LvBohemian " <LvBohemian@dis cussions.micros oft.comwrote in message
            news:AC603AFB-C1B2-4709-ACD0-5D451A33F28C@mi crosoft.com...
            >I hope this explains it better...
            I'm afraid it doesn't...
            I want the menu to stick around in case I want to load a different content
            page instead in the same MasterPage.
            Still seems to me that you're doing this backwards...

            Menus don't really work the same way as TreeViews...

            To change the ContentPage, you simply navigate to the other ContentPage - it
            will automatically pick up its MasterPage and redraw the menu for you...


            Comment

            • LvBohemian

              #7
              Re: MasterPages - Dynamically Created Menus Disappearing

              I am navigating to the applicable content page, using the NavigateUrl that is
              selected via the menu, when I do the menu disapears...

              Comment

              • Edwin Knoppert

                #8
                Re: MasterPages - Dynamically Created Menus Disappearing

                Dump a button on the masterpage and click on it..
                Will the menu dissapear as well?
                This is usually due not rebuilding the menu again on page load.

                sorry if i'm way off here.



                "LvBohemian " <LvBohemian@dis cussions.micros oft.comschreef in bericht
                news:DF2C7F7C-2C3A-4E2E-8DCB-9430C0D101AD@mi crosoft.com...
                >I am navigating to the applicable content page, using the NavigateUrl that
                >is
                selected via the menu, when I do the menu disapears...

                Comment

                • LvBohemian

                  #9
                  Re: MasterPages - Dynamically Created Menus Disappearing

                  thats how I am testing it, when I click on the button on the master page, I
                  dynamically build a menu, and it looks and works like I want it to at that
                  point...
                  then when I select a menu item which is wired to NavigateUrl to display the
                  applicable content page in the same master page's content place holder, it
                  shows the content page but the menu disappears...

                  thanks in advance.

                  Comment

                  • Mark Rae

                    #10
                    Re: MasterPages - Dynamically Created Menus Disappearing

                    "LvBohemian " <LvBohemian@dis cussions.micros oft.comwrote in message
                    news:DF2C7F7C-2C3A-4E2E-8DCB-9430C0D101AD@mi crosoft.com...
                    >I am navigating to the applicable content page, using the NavigateUrl that
                    >is
                    selected via the menu, when I do the menu disapears...
                    Ah well alright then - but that isn't, in fact what you actually said... You
                    said "...then when I select a menu item which loads the applicable
                    NavigateUrl into the MasterPage ContentPlaceHol der...", which is what I
                    thought you meant!

                    That's fine - you appear to be doing it correctly - you just have a problem
                    with nomenclature. Selecting a menu item doesn't load the applicable
                    NavigateUrl into the MasterPage ContentPlaceHol der - it's precisely the
                    reverse. Selecting a menu item navigates to the ContentPage which loads its
                    MasterPage.

                    However, that's by the by...

                    Back to the original question - where is the code which does the dynamic
                    menu creation...?


                    Comment

                    • LvBohemian

                      #11
                      Re: MasterPages - Dynamically Created Menus Disappearing

                      Here is the code behind for the master page...
                      using System;
                      using System.Data;
                      using System.Configur ation;
                      using System.Collecti ons;
                      using System.Web;
                      using System.Web.Secu rity;
                      using System.Web.UI;
                      using System.Web.UI.W ebControls;
                      using System.Web.UI.W ebControls.WebP arts;
                      using System.Web.UI.H tmlControls;

                      public partial class MsMasterMenu1 : System.Web.UI.M asterPage
                      {
                      public String ActiveMenu
                      {
                      get
                      {
                      object obj = ViewState["ActiveMenu "];
                      return (obj == null) ? String.Empty : (string)obj;
                      }
                      set
                      {
                      ViewState["ActiveMenu "] = value;
                      }
                      }

                      public String ActiveMenuConta iner
                      {
                      get
                      {
                      object obj = ViewState["ActiveMenuCont ainer"];
                      return (obj == null) ? String.Empty : (string)obj;
                      }
                      set
                      {
                      ViewState["ActiveMenuCont ainer"] = value;
                      }
                      }

                      public String ActiveMenuDataF ile
                      {
                      get
                      {
                      object obj = ViewState["ActiveMenuData File"];
                      return (obj == null) ? String.Empty : (string)obj;
                      }
                      set
                      {
                      ViewState["ActiveMenuData File"] = value;
                      }
                      }

                      public String ActiveMenuXPath
                      {
                      get
                      {
                      object obj = ViewState["ActiveMenuXPat h"];
                      return (obj == null) ? String.Empty : (string)obj;
                      }
                      set
                      {
                      ViewState["ActiveMenuXPat h"] = value;
                      }
                      }

                      protected void Page_Load(objec t sender, EventArgs e)
                      {
                      if (!IsPostBack)
                      {
                      ViewState.Add(" ActiveMenu", "");
                      ViewState.Add(" ActiveMenuConta iner", "");
                      ViewState.Add(" ActiveMenuDataF ile", "");
                      ViewState.Add(" ActiveMenuXPath ", "");
                      }

                      if (this.ViewState["ActiveMenu "].ToString() != "")
                      {
                      this.CreateMenu (this.ViewState["ActiveMenu "].ToString(),
                      this.ViewState["ActiveMenuCont ainer"].ToString(),
                      this.ViewState["ActiveMenuData File"].ToString(),
                      this.ViewState["ActiveMenuXPat h"].ToString());
                      }
                      }

                      public void CreateMenu(stri ng pstrMenuName, string
                      pstrActiveMenuC ontainer, string pstrActiveMenuD ataFile, string
                      pstrActiveMenuX Path)
                      {
                      Menu WebMenu = new Menu();
                      XmlDataSource xds = new XmlDataSource() ;
                      xds.DataFile = pstrActiveMenuD ataFile;
                      xds.ID = pstrMenuName + "XmlDs";
                      xds.XPath = pstrActiveMenuX Path;
                      WebMenu.Orienta tion = Orientation.Hor izontal;
                      WebMenu.DataSou rceID = xds.ID;
                      WebMenu.BackCol or =
                      System.Drawing. ColorTranslator .FromHtml("#B5C 7DE");
                      WebMenu.ForeCol or =
                      System.Drawing. ColorTranslator .FromHtml("#284 E98");
                      WebMenu.StaticM enuItemStyle.Ho rizontalPadding =
                      System.Web.UI.W ebControls.Unit .Pixel(5);
                      WebMenu.StaticM enuItemStyle.Ve rticalPadding =
                      System.Web.UI.W ebControls.Unit .Pixel(2);
                      WebMenu.Dynamic HoverStyle.Back Color =
                      System.Drawing. ColorTranslator .FromHtml("#284 E98");
                      WebMenu.Dynamic HoverStyle.Fore Color = System.Drawing. Color.White;
                      WebMenu.Dynamic MenuStyle.BackC olor =
                      System.Drawing. ColorTranslator .FromHtml("#B5C 7DE");
                      WebMenu.StaticS electedStyle.Ba ckColor =
                      System.Drawing. ColorTranslator .FromHtml("#507 CD1");
                      WebMenu.Dynamic SelectedStyle.B ackColor =
                      System.Drawing. ColorTranslator .FromHtml("#507 CD1");
                      WebMenu.Dynamic MenuItemStyle.H orizontalPaddin g =
                      System.Web.UI.W ebControls.Unit .Pixel(5);
                      WebMenu.Dynamic MenuItemStyle.V erticalPadding =
                      System.Web.UI.W ebControls.Unit .Pixel(2);
                      WebMenu.StaticH overStyle.BackC olor =
                      System.Drawing. ColorTranslator .FromHtml("#284 E98");
                      WebMenu.StaticH overStyle.ForeC olor = System.Drawing. Color.White;
                      WebMenu.StaticS ubMenuIndent =
                      System.Web.UI.W ebControls.Unit .Pixel(10);
                      WebMenu.Dynamic HorizontalOffse t = 10;

                      MenuItemBinding binding;
                      binding = SetItemBinding( "Menu", 0, "Text", "");
                      WebMenu.DataBin dings.Add(bindi ng);
                      binding = SetItemBinding( "Item", 1, "Text", "TargetUrl" );
                      WebMenu.DataBin dings.Add(bindi ng);

                      Control ctrlMenuContain er =
                      this.frmMain.Fi ndControl(pstrA ctiveMenuContai ner);
                      ctrlMenuContain er.Controls.Add (xds);
                      ctrlMenuContain er.Controls.Add (WebMenu);
                      }

                      protected MenuItemBinding SetItemBinding( String pstrDataMember, Int32
                      pintDepth, String pstrTextField, String pstrTargetUrlFi eld)
                      {
                      MenuItemBinding binding = new MenuItemBinding ();

                      binding.DataMem ber = pstrDataMember;
                      binding.Depth = pintDepth;
                      binding.TextFie ld = pstrTextField;
                      binding.Navigat eUrlField = pstrTargetUrlFi eld;

                      return binding;
                      }

                      protected void btnCreateMenu_C lick(object sender, EventArgs e)
                      {
                      this.ActiveMenu = "TestMenu";
                      this.ActiveMenu Container = "MasterMenuPane lA";
                      this.ActiveMenu DataFile = "~/Menu.xml";
                      this.ActiveMenu XPath = "/Root/Menu";

                      this.CreateMenu (this.ActiveMen u, this.ActiveMenu Container,
                      this.ActiveMenu DataFile, this.ActiveMenu XPath);
                      }
                      }

                      here is the html from the master page...
                      <%@ Master Language="C#" AutoEventWireup ="true"
                      CodeFile="MsMas terMenu1.master .cs" Inherits="MsMas terMenu1" %>
                      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">

                      <html xmlns="http://www.w3.org/1999/xhtml" >
                      <head runat="server">
                      <title>Test Master Menu Page</title>
                      </head>
                      <body>
                      <form id="frmMain" runat="server">
                      <div>
                      <table border="0" cellpadding="0" cellspacing="0" style="width: 100%;
                      height: 100%">
                      <tr>
                      <td colspan="2" style="height: 200px">
                      TODO: ...
                      </td>
                      </tr>
                      <tr>
                      <td style="width: 200px">
                      TODO: ... <br />
                      <asp:Button ID="btnCreateMe nu" runat="server"
                      OnClick="btnCre ateMenu_Click" Text="Create Menu" />
                      </td>
                      <td>
                      <asp:Panel ID="MasterMenuP anelA" runat="server" Height="50px"
                      Width="125px" >
                      </asp:Panel>
                      <asp:contentpla ceholder id="MainContent PlaceHolder"
                      runat="server">
                      </asp:contentplac eholder>
                      </td>
                      </tr>
                      <tr>
                      <td colspan="2" style="height: 200px">
                      TODO: ...
                      </td>
                      </tr>
                      </table>

                      </div>
                      </form>
                      </body>
                      </html>

                      here is the default consumer page code behind...
                      using System;
                      using System.Data;
                      using System.Configur ation;
                      using System.Collecti ons;
                      using System.Web;
                      using System.Web.Secu rity;
                      using System.Web.UI;
                      using System.Web.UI.W ebControls;
                      using System.Web.UI.W ebControls.WebP arts;
                      using System.Web.UI.H tmlControls;

                      public partial class ConsumeMsMaster Menu1 : System.Web.UI.P age
                      {
                      protected void Page_Load(objec t sender, EventArgs e)
                      {
                      }
                      }

                      here is the html for the default consumer page...
                      <%@ Page Language="C#" MasterPageFile= "~/MsMasterMenu1.m aster"
                      AutoEventWireup ="true" CodeFile="Consu meMsMasterMenu1 .aspx.cs"
                      Inherits="Consu meMsMasterMenu1 " Title="Untitled Page" %>
                      <%@ MasterType VirtualPath="~/MsMasterMenu1.m aster" %>
                      <asp:Content ID="ContentDefa ult"
                      ContentPlaceHol derID="MainCont entPlaceHolder" Runat="Server">
                      Default content page
                      </asp:Content>

                      here is the code behind for the select menu item content page...
                      using System;
                      using System.Data;
                      using System.Configur ation;
                      using System.Collecti ons;
                      using System.Web;
                      using System.Web.Secu rity;
                      using System.Web.UI;
                      using System.Web.UI.W ebControls;
                      using System.Web.UI.W ebControls.WebP arts;
                      using System.Web.UI.H tmlControls;

                      public partial class SelectMenuItemC ontentPage1 : System.Web.UI.P age
                      {
                      protected void Page_Load(objec t sender, EventArgs e)
                      {
                      }
                      }

                      here is the html for the select menu item content page...
                      <%@ Page Language="C#" MasterPageFile= "~/MsMasterMenu1.m aster"
                      AutoEventWireup ="true" CodeFile="Selec tMenuItemConten tPage1.aspx.cs"
                      Inherits="Selec tMenuItemConten tPage1" Title="Untitled Page" %>
                      <asp:Content ID="ContentMenu Item1"
                      ContentPlaceHol derID="MainCont entPlaceHolder" Runat="Server">
                      Selected item from menu content page
                      </asp:Content>


                      Comment

                      • Mark Rae

                        #12
                        Re: MasterPages - Dynamically Created Menus Disappearing

                        "LvBohemian " <LvBohemian@dis cussions.micros oft.comwrote in message
                        news:4C08F262-E7D9-4719-BBFF-BDEFCF98DE6E@mi crosoft.com...

                        Wow! Seems incredibly over-complex to me, but no matter...
                        protected void Page_Load(objec t sender, EventArgs e)
                        {
                        if (!IsPostBack)
                        Set a breakpoint on the above line, and run your site in Debug mode -
                        there's your problem...


                        Comment

                        • LvBohemian

                          #13
                          Re: MasterPages - Dynamically Created Menus Disappearing

                          I set a watch on the ActiveMenu and related properties as well as page load...

                          I am losing the values in ViewState as soon as I select the menu item
                          (setting NavigateUrlFiel d) and "before" it gets back to page load where you
                          might think all values might be zeroed out...

                          don't see where I am losing viewstate...

                          thanks in advance.


                          Comment

                          • BillE

                            #14
                            Re: MasterPages - Dynamically Created Menus Disappearing

                            I have a treeview in a master page which loads dynamically.

                            I have to rebuild the treeview on each postback, like Edwin said, or it will
                            be empty. It doesn't appear to be preserved in viewstate.

                            -Bill


                            "LvBohemian " <LvBohemian@dis cussions.micros oft.comwrote in message
                            news:29B06806-7E6E-4F6D-AA08-1D8AA0220147@mi crosoft.com...
                            >I am playing around with creating some menus
                            dynamically, and they
                            create fine and show up ok when I want them to; but
                            when I select a
                            menu NavigateUrl at run time, the applicable url shows
                            up as
                            anticipated in the content place holder but the menu
                            disappears...
                            >
                            the menu is part of the master page and is not in the
                            content place holder...
                            >
                            anybody have any ideas?
                            >
                            am I missing something obvious?
                            >
                            when I create the same menu at design time it does not
                            exhibit this behavior...
                            >
                            thanks in advance.
                            >

                            Comment

                            • LvBohemian

                              #15
                              Re: MasterPages - Dynamically Created Menus Disappearing

                              in order to rebuild the tree/menu on postback, I still need to persist the
                              values used to create the menu to begin with...
                              therein looks to be my problem, i am losing what I have previously put into
                              viewstate before I ever get to the page load/page init...

                              as soon as I select a menu item, bam, everything I put in viewstate is gone...

                              before the new page load/page init...

                              suggestions?

                              thanks in advance.

                              Comment

                              Working...