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

J

Johnny J.

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

Stan

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
 
J

Johnny J.

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

Guest

No, you can't.

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

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

Johnny J.

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.




"Anon User" <> skrev i meddelandet
No, you can't.

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

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

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.

"Anon User" <[email protected]> skrev i meddelandet
<script type="text/javascript">
var strSessionObject = '<%=Session["MySessionObject"].ToString()%>');
</script>

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top