Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

IE10 and ImageButtons

0.00/5 (No votes)
20 Nov 2012 2  
A fix for ImageButtons problem in IE10 when project is not 4.5

Introduction

Let's discover how Microsoft introduces a new concept: pixel splitting...

Background

For some reason, your ASP.NET 4.0 solution cannot be updated to 4.5 (that's my case). Using a lot of ImageButtons, the application now crashes in IE10 (Win8 but also Win7 preview).

After searching around, I found that the problem is caused by IE10 itself. When a user clicks on an imageButton, a NameValueCollection is created with a few key/values. Among these, we can find the coordinates of the clicked control.

But, for obscure reasons, IE10 sends this information in decimal (position of the control can be x: 18.66648424 y: 54.687142514 (???)).

ASP.NET is waiting for an integer value and logically fails when attempting to convert the string "18.66648424" into an integer.

Using the Code

I found a little fix for this by overriding "PostLoadData" method in ImageButton. So, all you need to do is to override this method with something like this:

protected override bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            // Control coordinates are sent in decimal by IE10 
            // Recreating the collection with corrected values            
            NameValueCollection modifiedPostCollection = new NameValueCollection();
            for (int i = 0; i < postCollection.Count; i++)
            {
                string actualKey = postCollection.GetKey(i);
                if (actualKey != null)
                {
                    string[] actualValueTab = postCollection.GetValues(i);
                    if (actualKey.EndsWith(".x") || actualKey.EndsWith(".y"))
                    {
                        string value = actualValueTab[0];
                        decimal dec;
                        Decimal.TryParse(value, out dec);
                        modifiedPostCollection.Add(actualKey, ((int)Math.Round(dec)).ToString());
                    }
                    else
                    {
                        foreach (string actualValue in actualValueTab)
                        {
                            modifiedPostCollection.Add(actualKey, actualValue);
                        }
                    }
                }
            }
            return base.LoadPostData(postDataKey, modifiedPostCollection);
        }

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here