系統Log顯示,某個運作多年的網站最近冒出以下錯誤:
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Web.UI.WebControls.ImageButton.LoadPostData(String postDataKey, NameValueCollection postCollection)
at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection)
程式久未更動,忽然出錯令人狐疑,後來找出關鍵點,當使用IE10瀏覽時才發生問題!
爬文佐以實驗,整理出以下心得:
- 發生條件: 1) 使用UpdatePanel包住ImageButton的ASP.NET網頁,2) IE10使用標準模式檢視(切成相容模式可避開)
- 遇到相同狀況的開發者不少,該問題在MS Connect已累計超過50票
- 問題根源為AJAX Client Library在IE10標準模式以UpdatePanel執行ImageButton送出,X、Y座標參數出現小數點,但ASP.NET PostBack接收時只接受整數
- ASP.NET 3.5/4.0都有此問題,ASP.NET 4.5則已修正
理論上只需調整AJAX Client Library或ASP.NET解讀X/Y值的邏輯即可解決,而目前已有Hotfix:
- 2783780 Hotfix rollup 2783780 is available for the .NET Framework 2.0 SP2 in Windows Server 2003 SP2 and Windows XP SP3
- 2784147 Hotfix rollup 2784147 is available for the .NET Framework 2.0 SP2 in Windows 7 and Windows Server 2008 R2
- 2783767 Hotfix rollup 2783767 is available for the .NET Framework 4
安裝後一帖見效,問題排除。