Click here to Skip to main content
15,900,461 members
Articles / Web Development / ASP.NET
Tip/Trick

IE10 and ImageButtons

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
20 Nov 2012CPOL 37.7K   4   11
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:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionToolTip should not be disappear after removing mouse pointer from that object. Pin
argeraju16-Feb-14 23:16
argeraju16-Feb-14 23:16 
QuestionImageButtons do not work in IE10 (i m using .net framework 2.0 ) Pin
argeraju9-Dec-13 18:17
argeraju9-Dec-13 18:17 
QuestionImageButtons do not work in IE10 (i m using .net framework 2.0 ) Pin
argeraju9-Dec-13 18:15
argeraju9-Dec-13 18:15 
QuestionWhere does the code go? Pin
Richard Creer17-Jun-13 4:29
Richard Creer17-Jun-13 4:29 
AnswerRe: Where does the code go? Pin
Arno197317-Jun-13 5:41
Arno197317-Jun-13 5:41 
QuestionThanks! Pin
Ashley van Gerven5-May-13 18:04
Ashley van Gerven5-May-13 18:04 
Questioni have used this code in ASP.NET, VB.NET Pin
Elayaraja Ponnusamy14-Apr-13 20:11
Elayaraja Ponnusamy14-Apr-13 20:11 
Questioncan't override LoadPostData Pin
Member 99673175-Apr-13 3:04
Member 99673175-Apr-13 3:04 
QuestionOverflowException Pin
MarjaR15-Mar-13 4:01
MarjaR15-Mar-13 4:01 
AnswerRe: OverflowException Pin
MarjaR16-Mar-13 3:03
MarjaR16-Mar-13 3:03 
I found out that the overflow exception was related to the regional settings on the server. In the conversion of the original string value to decimal, the decimal point was ignored and then the resulting numeric value is sometimes much to large for an Int32.

This is my correctly working modified version of the ImageButton.LoadPostData override:

C#
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 - 1; 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];
				// drop any decimals (taking the regional settings on the webserver into account)
				if (value.Contains(".")) {
					value = value.Substring(0, value.IndexOf("."));
				} else if (value.Contains(",")) {
					value = value.Substring(0, value.IndexOf(","));
				}
				modifiedPostCollection.Add(actualKey, value);
			} else {
				foreach (string actualValue in actualValueTab) {
					modifiedPostCollection.Add(actualKey, actualValue);
				}
			}
		}
	}
	return base.LoadPostData(postDataKey, modifiedPostCollection);
}


modified 16-Mar-13 9:18am.

QuestionPossible IE bug Pin
noname201521-Nov-12 16:31
noname201521-Nov-12 16:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.