在UpdatePanel中使用Menu出錯的解決方案

在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,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腳本錯誤的一個很好的思路。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章