在atlas的时候,在UpdatePanel中使用Menu控件是不会出错的,但是正式命名为asp.net ajax后,从beta1开始,在UpdatePanel 中就无法正常使用Menu控件了,一直到RTM也没有解决这个问题。在asp.net ajax的官方文档中也有这样的说明(原文http://ajax.asp.net/docs/overview/UpdatePanelOverv...):
The following ASP.NET controls are not compatible with partial-page updates, and are therefore not supported inside an UpdatePanel control:
TreeView and Menu controls.
Web Parts controls. For more information, see ASP.NET Web Parts Controls.
FileUpload controls when they are used to upload files as part of an asynchronous postback.
GridView and DetailsView controls when their EnableSortingAndPagingCallbacks property is set to true. The default is false.
Login, PasswordRecovery, ChangePassword, and CreateUserWizard controls whose contents have not been converted to editable templates.
The Substitution control.
Validation controls, which includes the BaseCompareValidator, BaseValidator, CompareValidator, CustomValidator, RangeValidator, RegularExpressionValidator, RequiredFieldValidator, and ValidationSummary control.
其它几个控件都有前提条件,唯独TreeView,Menu和WebParts控件都不支持。那是不是真的就没有办法了呢?
目前只遇到Menu无法使用,先来回放一下在UpdatePanel中使用Menu控件会出现什么样的脚本错误呢?
我是用Menu和MultiView来实现Tab控件的效果,但是在UpdatePanel中,首次转换选项页不会出错,但当我再次将鼠标移到选项按钮时就会出现:
'0.cells'为空或不是对象 的脚本错误提示。跟踪调试错误脚本
at http://localhost:15159/WebResource.axd?d=YEs0K3CeKJkuxCHKobf0Fg2&t=633046464802812500 [220]
Menu_HideItems
code: i < rows[0].cells.length
at http://localhost:15159/WebResource.axd?d=YEs0K3CeKJkuxCHKobf0Fg2&t=633046464802812500 [525]
Menu_SetRoot
code: Menu_HideItems()
at http://localhost:15159/WebResource.axd?d=YEs0K3CeKJkuxCHKobf0Fg2&t=633046464802812500 [39]
Menu_Expand
code: Menu_SetRoot(item)
at http://localhost:15159/WebResource.axd?d=YEs0K3CeKJkuxCHKobf0Fg2&t=633046464802812500 [297]
Menu_HoverStatic
code: Menu_Expand(node, data.horizontalOffset, data.verticalOffset)
at http://localhost:15159/MasterPage.aspx [26]
JScript - form1 anonymous function
code: el="stylesheet" /><lin
会发现是在Menu_HideItems函数内部出错了。可以根据脚本资源地址下载到脚本文件,得到Menu_HideItems原型定义。然后利javascript的却态特性,我们可以重写这个方法,来屏蔽这个错误。在官方论坛上找到这样一段js代码,把它拷到出错的页面上,就马上可以解决这个问题了。
var oldMenu_HideItems = Menu_HideItems; if(oldMenu_HideItems) { Menu_HideItems = function(items){ if (!items || ((typeof(items.tagName) == "undefined") && (items instanceof Event))) { items = __rootMenuItem; } if(items && items.rows && items.rows.length == 0){ items.insertRow(0); } return oldMenu_HideItems(items); } }
原文地址是:http://forums.asp.net/thread/1517884.aspx
我不知道这种办法能否也同样用在TreeView出错时,因为我没有在UpdatePanel中使用过TreeView。不过这种办法确实给我提供了解决控件内部js脚本错误的一个很好的思路。