IE10 sending image button click coordinates with decimals (floating point values) causing a ParseInt32 FormatException

后端 未结 10 1122
情深已故
情深已故 2020-12-08 07:48

It seems like ASP.NET 4.0 is not prepared to handle ImageButton events triggered by Internet Explorer 10. The problem is that IE10 sends the image click coordinates as doubl

10条回答
  •  天涯浪人
    2020-12-08 08:14

    As noted in another answer, this issue has been fixed in .NET 4.5.

    For those who can't upgrade to .NET 4.5, Microsoft has released an update to fix this problem for .NET 4.0 (KB2836939) and .NET 3.5 (KB2836942 and KB2836943).

    Here's how those KB articles describe the issue:

    When you click an ImageButton control that is inside an update panel on an ASP.NET-based webpage by using Internet Explorer 10 and later, the partial postback operation fails. Additionally, the server-side click event is not fired.

    For reference, here's the original ImageButton.LoadPostData code that throws FormatException:

    protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) {
        string name = UniqueID;
        string postX = postCollection[name + ".x"];
        string postY = postCollection[name + ".y"];
        if (postX != null && postY != null && postX.Length > 0 && postY.Length > 0) {
            x = Int32.Parse(postX, CultureInfo.InvariantCulture);
            y = Int32.Parse(postY, CultureInfo.InvariantCulture);
            if (Page != null) {
                Page.RegisterRequiresRaiseEvent(this);
            }
        }
        return false;
    }
    

    And here's the fixed code:

    protected virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection) { 
        string name = UniqueID;
        string postX = postCollection[name + ".x"];
        string postY = postCollection[name + ".y"];
        if (postX != null && postY != null && postX.Length > 0 && postY.Length > 0) {
            x = (int)ReadPositionFromPost(postX);
            y = (int)ReadPositionFromPost(postY);
            if (Page != null) {
                Page.RegisterRequiresRaiseEvent(this);
            }
        }
        return false;
    }
    
    internal static double ReadPositionFromPost(string requestValue) {
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Integer;
        return double.Parse(requestValue, style, CultureInfo.InvariantCulture);
    }
    

提交回复
热议问题