-1

I am using asp:menu. My aspx code is:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Menu ID="Menu_Library" runat="server">
        </asp:Menu>

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

I am generating the sub menu items (i.e) the childitems dynamically.. If i click the sub menu items it redirects me to a page which i specify like this in my code behind,

MenuItem childItem = new MenuItem();
childItem.NavigateUrl = "OtherPage.aspx";

But what i need is when i click on sub menu items, it should display some items in same page..

How to achieve this? Please help me.. It can be either in javascript or code behind.. I don't want it to navigate it to another page instead perform the action in same page..

Xavier
  • 1,672
  • 5
  • 27
  • 46

3 Answers3

1

Instead of using the Page link you can use javascript for that and any div you can show or hide in it.

Your Code:

MenuItem childItem = new MenuItem();
childItem.NavigateUrl = "OtherPage.aspx";

Change with:

MenuItem childItem = new MenuItem();
childItem.NavigateUrl = "javascript: return GoToSomeLink('"+ count +"');"; //You can pass parameters also

Javascript function:

<script type="text/javascript">
function GoToSomeLink(obj) //if parameters are used use them here also.
{
    var count=parseInt(obj); //use this count varible anywhere in the function
    $(#menuDiv).show(); /any div show or hide
    return false;

}
 </script>
Pratik
  • 1,472
  • 7
  • 20
  • 36
  • Where should i include this javascript function..? – Xavier Dec 17 '12 at 07:14
  • in the ASPX page under scripts tags. – Pratik Dec 17 '12 at 07:17
  • i have included inside aspx.. but its throwing me an script error..return' statement outside of function – Xavier Dec 17 '12 at 07:18
  • little updates in my answer as the javascript function is returning false means any server request in cancelled here – Pratik Dec 17 '12 at 07:21
  • childItem.NavigateUrl = "javascript:GoToSomeLink();"; gave me the solution – Xavier Dec 17 '12 at 07:34
  • how could i pass the argument inside that.. I have tried like "javascript:GoToSomeLink(count);" but i couldn achieve that as it is been placed inside quotes.. It is treating it as a string.. I need that int count to be passed to the javascript function – Xavier Dec 17 '12 at 11:03
  • 1
    Updated the answer ..give a try – Pratik Dec 17 '12 at 11:12
0

You can use load() or ajax() jquery methods to load the contents dynamically without refreshing the page.

OR you can just hide the related contents inline and show/hide them using toggle() method

Vivek S
  • 5,384
  • 8
  • 51
  • 72
0

You can use jquery.load method as follows

var a = this.find(".contentWrap"),
a.load(this.getTrigger().attr("href") + " .ajaxDiv");

The idea is this

  1. put any css-class to every menu/sub-menu on which you want to load another page say ajaxMenu.
  2. set the url of the menu/sub-menu through you code.
  3. create a container div on you mage page with css-class (conetntWrap)
  4. create a container div on every page which you want to load say .ajaxDiv. This is important because it will not load the _viewstate to your page.
  5. now write a function on document.ready as follow

    $(document).ready(function () 
    {
        $("a.ajaxMenu").live("click", function (a)
        {
            var a = this.find(".contentWrap"),
            a.load(this.getTrigger().attr("href") + " .ajaxDiv");
         });
     )};
    

    *---use classes on asp.net *

    <asp:Menu ID="NavigationMenu" 
         StaticMenuStyle-CssClass="StaticMenuStyle"
         StaticMenuItemStyle-CssClass="StaticMenuItemStyle"
         StaticSelectedStyle-CssClass="StaticSelectedStyle"
         StaticHoverStyle-CssClass="StaticHoverStyle"
        runat="server">
    </asp:Menu>
    

    the above are way to add css classe.

शेखर
  • 17,412
  • 13
  • 61
  • 117
  • what is contentWrap? i have not used any class like that.. I need to handle things on clicking the sub menu item..How to do that – Xavier Dec 17 '12 at 06:15
  • i generating the sub menu dynamically.. so i could not add a css class to it – Xavier Dec 17 '12 at 06:44