Hahn86
Hahn86

Reputation: 55

How to set onClick event in ASP Menu MenuItem

I have a dynamically generated menu (C#), like this:

MenuItem(string text, string value, string imageUrl, string navigateUrl, string target)

MenuItem AdminLevel1 = new MenuItem("Admin", "Admin");
MenuItem AdminPedidosRegisto = new MenuItem("Questions", "AdminQ");

NavigationMenu.Items.Add(new MenuItem("Messages Received", "AdminMessagesR", "", "./Admin/Messages.ascx", "ContainerIframe"));

AdminPedidosRegisto.ChildItems.Add(new MenuItem("Pending", "AdminPending", "", "./Admin/Pedidos.ascx", "ContainerIframe"));`

Where ContainerIframe is the iframe's ID and NavigationMenu is the (asp:Menu)'s ID.

I want to set some JavaScript to be executed when I click a MenuItem.

Is there a way?

Upvotes: 0

Views: 8939

Answers (4)

Jason Gilley
Jason Gilley

Reputation: 203

Hahns answer seems to make all items the same.

I use the following:

  1. Make the menu item non-selectable so it wont postback
  2. Add the following code where appropriate:

Page.ClientScript.RegisterStartupScript(typeof(Page), "123", string.Format("$('#MenuDiv').find('a').filter(':contains(\"{0}\")').css('cursor', 'pointer').click(function(){{ {1} }});", Title, Script), true);

Where:
"123" is a unique key for your script
MenuDiv is your menu client id
Title is the text you assigned to the menu item
Script is the javascript you want to run.

Upvotes: 0

Hahn86
Hahn86

Reputation: 55

Thanks to @Manibhadra (this is enough for parent items and child items)

window.onload = function ()
{
    var menuTable = document.getElementById("<%=NavigationMenu.ClientID%>");
    var menuLinks = menuTable.getElementsByTagName("a");
    for (i = 0; i < menuLinks.length; i++)
    {
        menuLinks[i].onclick = function () { setInnerHTML('ContainerTittle', this.innerHTML); }
    }
}

Upvotes: 0

lem2802
lem2802

Reputation: 1162

AdminLevel1.NavigateUrl = "javascript:callSomeFunction();";

Upvotes: 0

Manibhadra
Manibhadra

Reputation: 400

Try putting the following code inside head tag of your .aspx page:

   <script type="text/javascript"> window.onload = function(){

    var menuTable = document.getElementById("<%=Menu1.ClientID%>");  //specify your menu id instead of Menu1 var menuLinks = menuTable.getElementsByTagName("a");
        for(i=0;i<menuLinks.length;i++)
        {
            menuLinks[i].onclick = function(){return confirm("u sure to postback?");}
        }
        setOnClickForNextLevelMenuItems(menuTable.nextSibling); } function setOnClickForNextLevelMenuItems(currentMenuItemsContainer){

        var id = currentMenuItemsContainer.id;
        var len = id.length;
        if(id != null && typeof(id) != "undefined" && id.substring(0,4) == "Menu" && id.substring(parseInt(len)-5,parseInt(len)) == "Items")
        {
            var subMenuLinks = currentMenuItemsContainer.getElementsByTagName("a");
            for(i=0;i<subMenuLinks.length;i++)
            {
                subMenuLinks[i].onclick = function(){return confirm("u sure to postback?");}
            }
            setOnClickForNextLevelMenuItems(currentMenuItemsContainer.nextSibling);
        } } </script>

Note that you do not need to write any code in code behind file for this solution to work. I've tested the code in IE7 & FF2.0

Hope it works for you.

cheers!!

Upvotes: 1

Related Questions