Home > DeveloperSection > Forums > MenuItem call javascript function
Tanuj Kumar

Total Post:134

Points:940
Posted on    March-24-2015 7:47 AM

 JavaScript JQuery 
Ratings:


 1 Reply(s)
 722  View(s)
Rate this:

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

 

    function popWin(url) {

        var winHandle = window.open(url, '_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">

 </asp:Menu>

The code behind:

 

        string url = "/somepage.aspx";

        MenuItem child = new MenuItem();

        child.Text = "Some Text";

        child.NavigateUrl = "javascript:popWin('" + url + "');";

        //child.NavigateUrl = "javascript:window.open('somepage.aspx');";

        Menu1.Items[1].ChildItems.Add(child);

Just having some trouble calling the javascript function.

The window.open 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.



Jeet Verma
Jeet Verma

Total Post:110

Points:776
Posted on    March-24-2015 8:04 AM

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;

        Menu1.Items[1].ChildItems.Add(child);

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


Don't want to miss updates? Please click the below button!

Follow MindStick