1

I have asp.net Menu Item <asp:MenuItem NavigateUrl="" Text="Download" Value="Download"/>. When this item gets clicked, I want to execute jQuery click method. How can we do that?

2
  • Please post the HTML that control generates. Commented Mar 29, 2012 at 15:25
  • <tr onmouseover="Menu_HoverDynamic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="ctl00_Menu1n16"> <td><table class="ctl00_Menu1_7" cellpadding="0" cellspacing="0" border="0" width="100%"> <tr> <td style="white-space:nowrap;width:100%;"><a class="ctl00_Menu1_1 ctl00_Menu1_6" href="javascript:__doPostBack('ctl00$Menu1','Reports\\Download')">Download</a></td> </tr> </table></td> </tr> Commented Mar 29, 2012 at 15:41

2 Answers 2

4

Provide a CssClass property with some class to the menu item. On client side find the element using that class and attach click event handler to it.

<asp:MenuItem NavigateUrl="" CssClass="menuItem" Text="Download" Value="Download"/>

Js

$('.menuItem').click(function(){
    //do stuff here
});

Update:

I think you can specify the css class in this way.

<asp:Menu ID="mainMenu" runat="server">
    ..
    <asp:MenuItem NavigateUrl="" CssClass="menuItem" Text="Download" Value="Download"/>
    ..
    <StaticMenuItemStyle CssClass="menuItem" />
</asp:Menu>
Sign up to request clarification or add additional context in comments.

5 Comments

Or clientID can be used as a selector
Yes, ClientID can be used but you have to send the id to the client.
Validation (ASP.Net): Attribute 'CssClass' is not a valid attribute of element 'MenuItem'.
@ShankarSangoli Still the same error. I defined StaticMenuItemStyle.
Now, you can check another answer. Please go through it carefully.
2

Sample JQuery

<script language="javascript" type="text/javascript">
   $(function () {
        $(".MyMenu a").each(function (index) {
             $(this).click(function () {
                 alert(index);
                 return false;
             });
        });
   });
</script>

Modified HTML

<asp:Menu ID="_mainMenu" RenderingMode="Table" runat="server" CssClass="MyMenu" autopostback="true">
    <Items>
        <asp:MenuItem Text="Home" Value="Home"></asp:MenuItem>
        <asp:MenuItem Text="Index" Value="Home"></asp:MenuItem>
    </Items>
</asp:Menu>

Note - RenderingMode="Table"

4 Comments

Above function executing when you root menu , not menu item. If we have 2 or 3 menu item... how to mange the click?
I have added one more MenuItem. Now you can check, on clicking the Index will show alert value 1 and on clicking the Home will show the value 0.
Rendering the menu as a table is a very bad practice.
Sorry.. @PankajGarg I have sub menu item in menu item... Thats the reason I am not getting sub menu item index. I am able to get root menu item index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.