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. Advertising

  2. Johnny J.

    Stan Guest

    On 8 Feb, 14:47, "Johnny J." <> wrote:
    > 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.


    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. Advertising

  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.



    "Stan" <> skrev i meddelandet
    news:...
    > On 8 Feb, 14:47, "Johnny J." <> wrote:
    >> 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.

    >
    > Hi Johnny J
    >
    > Use the Session object to store information that needs to be preserved
    > between page requests.
    >
    > HTH
    Johnny J., Feb 9, 2009
    #3
  4. On Feb 9, 9:35 am, "Johnny J." <> wrote:
    > Can you access session variables from javascript?


    No, you can't.
    Alexey Smirnov, Feb 9, 2009
    #4
  5. On Feb 9, 8:56 pm, "Mark Rae [MVP]" <> wrote:
    > "Alexey Smirnov" <> wrote in message
    >
    > news:...
    >
    > >> Can you access session variables from javascript?

    >
    > > No, you can't.

    >
    > <script type="text/javascript">
    >     var strSessionObject = '<%=Session["MySessionObject"].ToString()%>');
    > </script>
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    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
    news:...
    On Feb 9, 8:56 pm, "Mark Rae [MVP]" <> wrote:
    > "Alexey Smirnov" <> wrote in message
    >
    > news:...
    >
    > >> Can you access session variables from javascript?

    >
    > > No, you can't.

    >
    > <script type="text/javascript">
    > var strSessionObject = '<%=Session["MySessionObject"].ToString()%>');
    > </script>
    >
    > --
    > Mark Rae
    > ASP.NET MVPhttp://www.markrae.net


    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. On Feb 10, 7:58 am, "Johnny J." <>
    wrote:
    > 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 meddelandetnews:...
    > On Feb 9, 8:56 pm, "Mark Rae [MVP]" <> wrote:
    >
    > > "Alexey Smirnov" <> wrote in message

    >
    > >news:....

    >
    > > >> Can you access session variables from javascript?

    >
    > > > No, you can't.

    >
    > > <script type="text/javascript">
    > > var strSessionObject = '<%=Session["MySessionObject"].ToString()%>');
    > > </script>

    >
    > > --
    > > Mark Rae
    > > ASP.NET MVPhttp://www.markrae.net

    >
    > 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".


    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. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Oliver

    newbie - stupid install question

    Mark Oliver, Nov 20, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    372
    Mark Oliver
    Nov 20, 2004
  2. Brandon McCombs
    Replies:
    4
    Views:
    502
    Richard Wheeldon
    Aug 28, 2006
  3. \(ProteanThread\)
    Replies:
    1
    Views:
    340
    Walter Roberson
    Mar 10, 2005
  4. Replies:
    0
    Views:
    543
  5. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,011
Loading...

Share This Page