Thursday, September 26, 2013

ImageButton Error Fix for IE10

Problem:

When clicking an ImageButton in IE10, you get the following error:

Exception message: 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.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad)

at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


This error is caused because the Microsoft Ajax Framework is sending the mouse coordinates as a floating point number instead of an integer. The solution for this is to move your website to IIS7 or greater.

Some time ago, I was unable to upgrade my IIS server so I came up with a solution to fix the problem and still keep IIS6.

Some people resorted to creating an alternate ImageButton which inherited from the base ImageButton class. Although this was ok, it was not good enough for me because that meant I had to update way too many pages and way too many websites hosted on the same server. Therefore, I resorted to creating my own solution which would be global per site.

What did I do? Here is the code. All you have to do is place it inside the Global.asax file:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 void Application_Start(object sender, EventArgs e) { //Code that runs on application startup } void Application_End(object sender, EventArgs e) { //Code that runs on application shutdown } //This function runs after .Net has assigned a handler for the current request. //The purpose of this function is to attach a Load event to the current handler (page). void Application_PostMapRequestHandler(object sender, EventArgs e) { var app = (HttpApplication)sender; var handler = (IHttpHandler)app.Context.Handler; Page page = handler as Page; if (page != null) { page.Load += Page_Load; //Subscribes the below function to the page load event. } } //This is the function that will run on every page load for the entire site. private void Page_Load(object sender, EventArgs e) { var page = (Page)HttpContext.Current.Handler; var smg = ScriptManager.GetCurrent(page); if (smg != null && (page.IsPostBack == false || smg.IsInAsyncPostBack == false)) { //This is a javascript function that is created on the fly. //This function removes all numbers after the decimal point. var jsFunction = "function IE10ImgFloatFix() {try {var o = Sys.WebForms.PageRequestManager._instance;var s = o._additionalInput;s = s.replace(/(.y=\\d+)([.]\\d+)?/g, '$1');s = s.replace(/(.x=\\d+)([.]\\d+)?/g, '$1');o._additionalInput = s;} catch (ex){}}"; //This part will place the jsFunction code on every page of your site. ScriptManager.RegisterStartupScript(page, page.GetType(), "IE10ImgFloatFix_func", jsFunction, true); //This code registers a function call to your jsFunction. //When a person clicks on any ImageButton on any of your pages, your jsFunction gets called. ScriptManager.RegisterOnSubmitStatement(page, page.GetType(), "IE10ImgFloatFix_call", "IE10ImgFloatFix();"); } }


Here is the VB.NET code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) 'Code that runs on application startup End Sub Sub Application_End(ByVal sender As Object, ByVal e As EventArgs) 'Code that runs on application shutdown End Sub 'This function runs after .Net has assigned a handler for the current request. 'The purpose of this function is to attach a Load event to the current handler (page). Sub Application_PostMapRequestHandler(sender As Object, e As EventArgs) Dim app = CType(sender, HttpApplication) Dim handler = CType(app.Context.Handler, IHttpHandler) Dim page = CType(handler, Page) If page IsNot Nothing Then AddHandler page.Load, AddressOf Page_Load End If End Sub 'This is the function that will run on every page load for the entire site. Private Sub Page_Load(sender As Object, e As EventArgs) Dim page = CType(HttpContext.Current.Handler, Page) Dim smg = ScriptManager.GetCurrent(page) If smg IsNot Nothing AndAlso (page.IsPostBack = False OrElse smg.IsInAsyncPostBack = False) Then 'This is a javascript function that is created on the fly. 'This function removes all numbers after the decimal point. Dim jsFunction = "function IE10ImgFloatFix() {try {var o = Sys.WebForms.PageRequestManager._instance;var s = o._additionalInput;s = s.replace(/(.y=\\d+)([.]\\d+)?/g, '$1');s = s.replace(/(.x=\\d+)([.]\\d+)?/g, '$1');o._additionalInput = s;} catch (ex){}}" 'This part will place the jsFunction code on every page of your site. ScriptManager.RegisterStartupScript(page, page.GetType(), "IE10ImgFloatFix_func", jsFunction, True) 'This code registers a function call to your jsFunction. 'When a person clicks on any ImageButton on any of your pages, your jsFunction gets called. ScriptManager.RegisterOnSubmitStatement(page, page.GetType(), "IE10ImgFloatFix_call", "IE10ImgFloatFix();") End If End Sub

No comments:

Post a Comment