Skip to content Skip to sidebar Skip to footer

Ie10 Sending Image Button Click Coordinates With Decimals (floating Point Values) Causing A Parseint32 Formatexception

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 double value

Solution 1:

There are hotfixes for .NET CLR 2.0 and 4.0, as described in this blog entry by Scott Hanselmann:

What the fixes do is update the ie.browser and firefox.browser files in \Windows\Microsoft.NET\Framework\<version>\Config\Browsers with new and future-proofed versions of these browser definitions. Nothing else is affected.

.NET 4

.NET 2.0

Alternatively, there's a client-based javascript patch (originally posted as workaround on the Connect item with bug ID:755419):

$(function () {
    // Patch fractional .x, .y form parameters for IE10.if (typeof (Sys) !== 'undefined' && Sys.Browser.agent === Sys.Browser.InternetExplorer && Sys.Browser.version === 10) {
        Sys.WebForms.PageRequestManager.getInstance()._onFormElementActive = functionSys$WebForms$PageRequestManager$_onFormElementActive(element, offsetX, offsetY) {
            if (element.disabled) {
                return;
            }
            this._activeElement = element;
            this._postBackSettings = this._getPostBackSettings(element, element.name);
            if (element.name) {
                var tagName = element.tagName.toUpperCase();
                if (tagName === 'INPUT') {
                    vartype = element.type;
                    if (type === 'submit') {
                        this._additionalInput = encodeURIComponent(element.name) + '=' + encodeURIComponent(element.value);
                    }
                    elseif (type === 'image') {
                        this._additionalInput = encodeURIComponent(element.name) + '.x=' + Math.floor(offsetX) + '&' + encodeURIComponent(element.name) + '.y=' + Math.floor(offsetY);
                    }
                }
                elseif ((tagName === 'BUTTON') && (element.name.length !== 0) && (element.type === 'submit')) {
                    this._additionalInput = encodeURIComponent(element.name) + '=' + encodeURIComponent(element.value);
                }
            }
        };
    }
});

Solution 2:

Simply installing .NET Framework 4.5 can fix this problem.

This can fix the problem even if you do not switch your application pool over to .NET Framework 4.5.

In my case, I left the app pools at .NET Framework 3.5. Apparently installing .NET Framework 4.5 overwrites some files for other framework versions.

Since it is so easy to install the new .NET Framework version, it's probably worth a try before bothering with the hotfixes (which did not work for me) or other solutions.

See the workarounds section here

Solution 3:

Here's a JavaScript workaround. It overrides the existing method, floors the x and y coordinates then calls the existing method with these new coordinates.

Sys.WebForms.PageRequestManager.getInstance()._origOnFormActiveElement = Sys.WebForms.PageRequestManager.getInstance()._onFormElementActive;
Sys.WebForms.PageRequestManager.getInstance()._onFormElementActive = function(element, offsetX, offsetY){
    if (element.tagName.toUpperCase() === 'INPUT' && element.type === 'image'){
        offsetX = Math.floor(offsetX);
        offsetY = Math.floor(offsetY);
    }
    this._origOnFormActiveElement(element, offsetX, offsetY);
};

Solution 4:

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:

protectedvirtualboolLoadPostData(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);
        }
    }
    returnfalse;
}

And here's the fixed code:

protectedvirtualboolLoadPostData(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);
        }
    }
    returnfalse;
}

internalstaticdoubleReadPositionFromPost(string requestValue) {
    NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.Integer;
    returndouble.Parse(requestValue, style, CultureInfo.InvariantCulture);
}

Solution 5:

If you press F12 and switch to IE9 manually, it works like a charm. So our apporach was to use content="IE=9" but this only switches the document mode in IE10 not the browser mode and that seems to be not enough.

Maybe someone has an idea on how to switch the document mode too?

Another workaround that gets more and more popular is to overwrite LoadPostData, see

http://www.codeproject.com/Tips/496162/IE10-and-ImageButtons?display=Mobilehttp://forums.asp.net/t/1823287.aspx/2/10

Personally I woulve have found the content="IE=9" the best solution because of the little additional work and impact.

Post a Comment for "Ie10 Sending Image Button Click Coordinates With Decimals (floating Point Values) Causing A Parseint32 Formatexception"