Wednesday, March 28, 2012

UpdatePanel - Input string was not in a correct format

I have a simple function to expand/contract a div in an update panel to show/hide the edit controls on a page:

protectedvoid btnExpand_Click(object sender,ImageClickEventArgs e){

divControls.Visible = !divControls.Visible;

}

most of the time it works, maybe 95%, but sometimes I get that error "Input string was not in a correct format". If I try to handle the error withOnAsyncPostBackError="ScriptManager1_AsyncPostBackError" , I can grab more info but I have no idea where the error is:
source: mscorlib
stack trace: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.Int32.Parse(String s, IFormatProvider provider) at System.Web.UI.WebControls.ImageButton.LoadPostData(String postDataKey, NameValueCollection postCollection) at System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackDataHandler.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)
targetsite: {Void StringToNumber(System.String, System.Globalization.NumberStyles, NumberBuffer ByRef, System.Globalization.NumberFormatInfo, Boolean)}

it may be an issue with trying to start a new request before finishing the last request. Any ideas?

We are running into the same problem. I've worked out that what's happening is, if an ImageButton is inside an UpdatePanel then something is happening when the user clicks on it.

If they click on the very top row of pixels, or the very left row of pixels on the button, the browser is posting "NaN" for the x or y coordinate of the click, and the server is barfing when it goes to parse it.

I can see this when I look at the request in Fiddler:

btnButton.x=55
btnButton.y=NaN

If the button isnot in an UpdatePanel, then it's fine (it posts 0 as the coordinate). If the user doesn't click on the very top or the very left of the image, then it's also fine.


There is another thread covering this issue here:http://forums.asp.net/thread/1458229.aspx

One on the ASP.NET team members has required some trace code, which I posted and I'm still waiting for an answer. They should be investigating the issue

Regards,

Juan


awesome, thanks guys, at least now I know why the problem was so intermittent, my button is + - so it's really small, now if only we can get M$ to fix it...

i saw a hundred of these threads, but most of them had people actually with type mismatches, hard to sift through them all


If you put BorderWidth="1px" and BorderColor="Background Color" in the <asp:ImageButton> properties I never got the error again. Hope this works for you.

Is there any update on this bug? I tried the border stuff, and it works nicely - I can't generate the error any more.

However, as a general solution, it's obviously not very good. In this particular case for us, a 1px background colour border is not a big deal, but I can certainly imagine cases where a 1 pixel borderisa big deal (like if we had a precise layout, or something).

So can someone from Microsoft please confirm that thisisan actual bug, and let us know when it'll be fixed (or has it already?)

I can provide a fairly simple test case if required.


Very useful information on this error guys, cheers. I'm eager to hear the reponse to this problem also as i'm one of those people which cannot add a 1px border to my image buttons because of the precise layout.

Smile

Thanks.


I've tried solution with 1px border but it didn't worked for me. Then I applied the following fix:

<asp:ImageButton ID="ImageButton1" runat="server" OnClientClick="if (event.offsetX==0 || event.offsetY==0) return false; " />

Very useful information

thnxSmile


Useful information......

thnxSmile


I am in SHOCK! I didn't think I would ever figure this one out. What a WEIRD bug!

Thanks for figuring this out! You deserve a raise.


For people that can't do the border hack the following may be useful.

using System.Collections.Specialized;using System.Runtime.InteropServices;using System.Web.UI;[assembly : TagPrefix("MS.Utilities.CustomControls","MS")]namespace MS.Utilities.CustomControls{/// <summary> /// To avoid issue when Update Panel can return NaN instead of 0 /// as discussed here http://forums.asp.net/thread/1450669.aspx /// </summary> [ToolboxData("<{0}:ImageButton runat=server></{0}:ImageButton>")] [ComVisible(false)]public class ImageButton : System.Web.UI.WebControls.ImageButton {protected override bool LoadPostData(string postDataKey, NameValueCollection postCollection) {string xKey;string yKey;string xValue = postCollection[xKey = (string.Format("{0}.x", UniqueID))];string yValue = postCollection[yKey = (string.Format("{0}.y", UniqueID))];if (!string.IsNullOrEmpty(xValue) && !string.IsNullOrEmpty(yValue)) { NameValueCollection postCollectionCopy =new NameValueCollection();int x;int y;int.TryParse(xValue,out x);int.TryParse(yValue,out y); postCollectionCopy.Add(xKey, x.ToString()); postCollectionCopy.Add(yKey, y.ToString());return base.LoadPostData(postDataKey, postCollectionCopy); }else {return false; } } }}

No comments:

Post a Comment