MenuItem call javascript function

Total Post:134


 1796  View(s)
Rate this:

Dynamically building menu items and need to call a javascript function from one of them. 

    function popWin(url) {
        var winHandle=, '_blank', 'width=1000,height=700,resizable=yes,top=5,left=5,scrollbars=yes,status=yes');
 <asp:Menu ID="Menu1" runat="server" StaticDisplayLevels="8" RenderingMode="List" OnMenuItemClick="Menu1_MenuItemClick" CssClass="menu" Width="150px">

The code behind: 

        string url ="/somepage.aspx";
        MenuItem child= new MenuItem();
        child.Text ="Some Text";
        child.NavigateUrl = "javascript:popWin('" + url + "');";
        //child.NavigateUrl ="'somepage.aspx');";

Just having some trouble calling the javascript function.

The will work but an extra page opens and I need more control over the window opening. 

The reason this needs to be done this way is there's an iFrame on the page that's loading different pages. The RegisterStartupScript was being used in the Menu1_MenuItemClick event but it was refreshing the source of the iFrame. 

Thanks for any suggestions.

  1. Re: MenuItem call javascript function

    After coming back to this problem and testing a span tag in html with an onclick event, the problem was simply a bit of syntax: 

            child = new MenuItem();
            string win ="/SomePage.aspx";
            string script= "popWin('" + win + "');";
            string text ="<span style=\"cursor:pointer;\" onclick=\"" + script + "\">Some Text</span>";
            //Below is how it should render
            //<span style="cursor:pointer;" onclick="popWin('/SomePage.aspx');">Some Text</span>
            child.Text =text;
            child.Selectable = false;

    Thought I would post this just in case anyone else runs into the same issue.

      Modified On Apr-09-2018 02:33:12 AM


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.