How do I? Stupid newbie question I'm sure...

Discussion in 'ASP .Net' started by Johnny J., Feb 8, 2009.

  1. Johnny J.

    Johnny J. Guest

    On my Master Page, I've got a menu on the left hand side which is basically
    an unordered list of links. some of the the menu links have got submenus
    which are nested unordered lists embedded in a <div> that I can show or hide
    using javascript depending on whether or not the submenu is expanded or not.

    If a submenu is open, I want to keep it open until the user decides to close
    it.

    What I've done until now is:

    I've placed a hidden text field on the master page in which I record the div
    id of the currently open submenu, and when the master page is submitted I
    reopen the submenu with the id in the hidden field.

    This works perfectly when submitting the master page itself, but if I click
    another menu link to open another page that uses the same master page, then
    the contents of the hidden field is not preserved (obviously enough because
    the master page form is not submitted).

    How can I transfer the id of the open submenu to the same master page
    embedding another page - so that I can reopen the sub menu there as well?

    I'm sure there's a best practise for such basic behaviour as this, but I'm
    new to web programming (have programmed winforms for 20 years though).

    TIA,
    Johnny J.
     
    Johnny J., Feb 8, 2009
    #1
    1. Advertisements

  2. Johnny J.

    Stan Guest

    Hi Johnny J

    Use the Session object to store information that needs to be preserved
    between page requests.

    HTH
     
    Stan, Feb 8, 2009
    #2
    1. Advertisements

  3. Johnny J.

    Johnny J. Guest

    I Thought about that, but the problem is that the value of the control ís
    changed using javascript. Can you access session variables from javascript?
    If so, what's the syntax?

    I also tried saving the value of the open menu to a session variable in the
    page's Unload event to restore it in the new pages Load event. But strangely
    enought, the new Load event is fired BEFORE the old Unload event. I don't
    get the logic of that.

    Cheers,
    Johnny J.
     
    Johnny J., Feb 9, 2009
    #3
  4. No, you can't.
     
    Alexey Smirnov, Feb 9, 2009
    #4
  5. Mark, you're right, this would work. However it is not an answer and
    make no sense to me. For example, this would not work if user clicked
    to a link from another site. I would suggest to identify submenu by
    the link of the current page:

    Add id and runat="server" to the <div>

    <div id="M0" runat="server" ...

    Create css class to show/hide <div>

    Add to master page

    int menuId;
    string[] menuItems = new string[] { "M0", "M1", ...}; // List of <div>
    objects

    // get name of current page
    string p = Request.Url.Segments[1].ToLower().Replace("/",
    string.Empty);
    switch (p)
    {
    case "contacts.aspx":
    menuId = 1;
    break;
    case "feedback.aspx":
    menuId = 2;
    break;
    ....
    }

    // find <div> and assign style
    HtmlAnchor t = (HtmlAnchor)this.FindControl(menuItems[menuId]);
    t.Attributes.Add("class", "showMenuStyle");

    This should assign to particular <div> object a style with name
    "showMenuStyle".
     
    Alexey Smirnov, Feb 9, 2009
    #5
  6. Johnny J.

    Johnny J. Guest

    Hi all

    Thanks a lot for your input. I'm still looking for a good way to do this.
    Alexey, you've got a good idea there. I had already thought about doing it
    that way. The problem is that the menu is meant to be user definable. I want
    the user to be able to add new menu items and submenu items. In that case
    they will all point to the same page, e.g. userpage.aspx?id=XXX. The
    contents of the page is simple text and is saved in a database table with
    the id number.

    So you see: Because those pages can appear in any menu/submenu, I cannot
    identify the submenu on the basis of "userpage.aspx" alone. It would entail
    building some extra logic to find out to which submenu (or main menu) item
    the id is associated.

    I might try to see it I can do it this way anyway though. So far it seems
    like the best solution (if not very easy).

    Thanks all of you,
    Johnny J.




    "Alexey Smirnov" <> skrev i meddelandet
    Mark, you're right, this would work. However it is not an answer and
    make no sense to me. For example, this would not work if user clicked
    to a link from another site. I would suggest to identify submenu by
    the link of the current page:

    Add id and runat="server" to the <div>

    <div id="M0" runat="server" ...

    Create css class to show/hide <div>

    Add to master page

    int menuId;
    string[] menuItems = new string[] { "M0", "M1", ...}; // List of <div>
    objects

    // get name of current page
    string p = Request.Url.Segments[1].ToLower().Replace("/",
    string.Empty);
    switch (p)
    {
    case "contacts.aspx":
    menuId = 1;
    break;
    case "feedback.aspx":
    menuId = 2;
    break;
    ....
    }

    // find <div> and assign style
    HtmlAnchor t = (HtmlAnchor)this.FindControl(menuItems[menuId]);
    t.Attributes.Add("class", "showMenuStyle");

    This should assign to particular <div> object a style with name
    "showMenuStyle".
     
    Johnny J., Feb 10, 2009
    #6
  7. Yes, it really depends on implementation. And I don't see how session
    will help here. You may consider using an additional javascript or
    asp.net "injection" to supply additional code to menu links. For
    example if link is

    <a href="feedback.aspx">Feedback</a>

    you can add

    onclick="go(this)"

    to call a function to redirect user to "feedback.aspx?menu=submenu2"

    function go(obj) {
    var parent = obj.parentNode.id;
    var href = this.href;
    var new_url = href + "?menu=" + parent;
    document.location.href = new_url;
    }

    This is maybe not a perfect way, but I think it could help.
     
    Alexey Smirnov, Feb 10, 2009
    #7
  8. How about using cookies?

    Andrew
     
    Andrew Morton, Feb 11, 2009
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.