开发者

Disable javascript generation by ASP.NET menu control

In my website I'm using the standard ASP.NET menu control. I already got so far as to write a custom control adapter to get rid of the rather tacky html output that is generated by the default control adapter.

One thing keeps buggering me though. Somehow ASP.NET is generating extra javascript that I don't want nor need for my menu controls, since I won't be using any of the dynamic features in the control. I replaced the control adapter, so it doesn't generate compatible HTML for that.

Anyone got an idea how I 开发者_开发技巧can prevent ASP.NET from generating the extra javascript for the menu control?


This problem cropped up for me after upgraded to ASP.net 4.0 with the installation of vs 2010. The fix is to either force the menu to render as a table or to turn off the new CSS/javascript "features" that 4.0 adds. Settings the menu's RenderingMode attribute to "Table" fixed this problem for me even though I use a Menu Adapter to render the control with lists.

<asp:Menu ID="mnuStuff" runat="server" RenderingMode="Table">
    ...
</asp:Menu>

You can do this site wide setting controlRenderingCompatibilityVersion to 3.5 in the web.config:

<system.web> 
  <pages controlRenderingCompatibilityVersion="3.5"/> 
</system.web>

This will eliminate the rendering of inline javascript that asp injects in the base of the page.


If you prefer to stick with ASP.Net 4.0 control rendering you can create a custom menu (derived from System.Web.UI.WebControls.Menu) and replace the OnPreRender:

public class MyCustomMenu : Menu
{
    protected override void OnPreRender(EventArgs e)
    {
        // Don't call base OnPreRender
        //base.OnPreRender(e);
    }
}

That did the trick.


An alternative way to get rid of the menu startup script is to call RegisterStartup script method before the Menu PreRender event, using the same script key, and outputting dummy (or empty) script.

This relies on internal implementation details of the Menu Type discovered using Reflector, so is somewhat fragile.

For example, a static class that looks something like:

static MenuHelper
{
    private static Type _rendererType = 
          typeof(Menu).Assembly.GetType(
              typeof(Menu).FullName + "+MenuRendererStandards"
              );

    public static void RemoveMenuScript(Menu menu)
    {
        string dummyScript = "<!-- Removed Menu Startup script -->";
        string key = "_registerMenu_" + menu.ClientID;
        ScriptManager.RegisterStartupScript(menu, _rendererType, key, dummyScript, false);
    }
}

You then just need to make sure you call MenuHelper.RemoveMenuScript(menu) before the menu's PreRender event.

The OP is using an Adapter so Tim Santeford's answer is better in his situation. But if you want to render a static menu as a list without the startup code, and without the effort of writing an adapter, this might be an alternative.


<script type="text/javascript">
    Sys.WebForms.Menu = "";
</script>

it works..

use it in aspx page


I just plan to ask about a similar question after 2 hours of search and have no luck.

What I want is to use the jquery superfish plugin as I want its animation for smooth looking. And with the ASP.NET generated javascript, the superfish just won't work.

And finally I try to set the ASP.NET Menu's control's attribute Enabled = false

and the outcoming source becomes this:

new Sys.WebForms.Menu({ element: 'mysitemeun', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: **true** });

After tracing the code, setting it disabled still have some style changes in the Menu but it will not add event to the MenuItem. And the superfish's animation works now.

if (!this.container.disabled) {
    Sys.WebForms.Menu._domHelper.addEvent(this.element, 'mouseover', Sys.WebForms.MenuItem._onmouseover);
    Sys.WebForms.Menu._domHelper.addEvent(this.element, 'mouseout', Sys.WebForms.MenuItem._onmouseout);
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜