开发者

Remove, replace or disable dynamically-generated ASP.Net js code

I am working with a few .Net 4.0 webforms controls such as the Menu control and while I think it's great that I can now declare the way in which controls are rendered (i.e. as either tables or divs), I can't switch off the automagically-included javascript that manages the ho开发者_运维百科ver events for those controls, for example:

new Sys.WebForms.Menu({ element: 'NavigationMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false }

This appears at the bottom of every page that owns such a control.

Given there is no way to actually switch this off, what is the best approach to either disabling this script, overriding it or in some other way rendering it impotent so that I can define my own jQuery methods in its place?


Well, what you can try is include some javascript after the Webforms scripts are included that does the following:

Sys.WebForms.Menu = function() {};

Basically, that overrides the webforms Menu javascript to do nothing. Be careful though, especially if your working on an existing project that uses these menus already, as they're javascript will no longer work.

Personally, I would recommend creating your own menu markup, css and javascript using a much "leaner" control like the Repeater or ListView. This would avoid most of these issues and you would have much more control over the output.


Just ran into (or just noticed) similar problem with the legacy code in our app.

Like Phil.Wheeler, also using a Sitemap data source. Not sure that changing the rendering mode to 3.5 is a good thing for us and the script hack to override Sys.WebForms.Menu did not work.

Problem:

This code is inserted auto-magically on every aspx page:

<script type='text/javascript'>new Sys.WebForms.Menu({ element: 'ctl00_MainNavMenu', disappearAfter: 500, orientation: 'horizontal', tabIndex: 0, disabled: false });</script>

None of our pages has an element with id of 'ct100_MainNavMenu' so, we are seeing a javascript error in MenuStandards.js resolving tagName === 'DIV'. this.element is null.

Sys.WebForms.Menu = function(options) {
    this.items = [];
    this.depth = options.depth || 1;
    this.parentMenuItem = options.parentMenuItem;
    this.element = Sys.WebForms.Menu._domHelper.getElement(options.element);
    if (this.element.tagName === 'DIV') {
        var containerElement = this.element;
        this.element = Sys.WebForms.Menu._domHelper.firstChild(containerElement);
        this.element.tabIndex = options.tabIndex || 0;
        options.element = containerElement;
        options.menu = this;
        this.container = new Sys.WebForms._MenuContainer(options);
        Sys.WebForms.Menu._domHelper.setFloat(this.element, this.container.rightToLeft ? "right" : "left");
    }
    else {
        this.container = options.container;
        this.keyMap = options.keyMap;
    }

Adding the following to our ASPX master file, as the last html before the tag seems to work (it gets rid of the problem):

<div id="ctl00_MainNavMenu" style="display:none">
    <div id="neededToPreventSecondErrorAt_tabIndex"></div>
</div>

The rendered HTML looks like this:

<div id="ctl00_MainNavMenu" style="display: none; float: left;">
    <div tabindex="0" role="menubar" class="static" style="position: relative; width: auto; float: left;"></div>
</div>

Not seeing any ill effects on any of our ASPX pages, testing on IE, FF, and Chrome. Clearly, there'll be a problem if an element is ever created on the page with the same ID. Not sure how likely that is unless we redo our app menus. I don't think it's any worse/riskier than overriding the webforms Menu javascript.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜