PDA

View Full Version : [CLOSED] NumberField postback error



JulianPreece
Oct 02, 2012, 8:39 AM
Hi,

When using a number field if you enter 2 dots as the value (..) it breaks all direct methods and direct events for the page. We can obviously work around this by checking if the formpanels/fields are valid client side but we should be able to catch the error server side!

Example code:

Default.aspx:-

<%@ Page CodeBehind="~/Default.aspx.cs" AutoEventWireup="true" Language="C#" Inherits="ext2testapp.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<ext:ResourceManager ID="resMan" runat="server">
</ext:ResourceManager>
<form id="Form1" runat="server">
<ext:NumberField runat="server" ID="test" />
<ext:Button runat="server" ID="btnTest" Text="click me">
<DirectEvents>
<Click OnEvent="btnClick" />
</DirectEvents>
<Listeners>
<Click Handler="#{DirectMethods}.stuff()" />
</Listeners>
</ext:Button>
</form>
</body>
</html>

Default.aspx.cs:-

public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnClick(object sender, DirectEventArgs e)
{
var num = test.Value;
}

[DirectMethod]
public void stuff()
{
var num = test.Value;
}
}

the error throws in numberfieldbase in the checkrange function (via LoadPostData), it hits Double.Parse("..", nf) and then hits the catch which throws an error, but on postback we cant catch it, instead wouldn't it be better to mark the field as invalid server side and set the value to its empty value so it doesn't break eveything on a postback?

Vladimir
Oct 02, 2012, 9:01 AM
If you update from 2.1 branch (http://svn.ext.net/premium/branches/2.1) then number field will fix the issue automatically (incorrect part of value (two dots and all symbols after two dots) will be removed)

In any way, you can catch the exception in global error handler (in Global.asax), just set RethrowAjaxExceptions="true" for ResourceManager

Vladimir
Oct 02, 2012, 9:02 AM
Second option, you can catch the error on the client side, use Failure handler of direct event

JulianPreece
Oct 02, 2012, 11:17 AM
I've just got the latest from the SVN and its still the same (revision 4395), if you run the example I gave it throws an exception within the catch:

throw new InvalidCastException("The Text value supplied is not a type of Double. " + exception.Message);

catching it in the global does work, but surely an invalid value in the field shouldn't break every direct method and event for the page?

If you do the same with a DateField and type anything into it the value in the code behind its DateTime.MinValue which is what I would expect, but a number field just breaks everything....

Vladimir
Oct 02, 2012, 11:26 AM
Please clarify Does nmber field reset invalid value on blur on your side?
if not then what browser do you use?

JulianPreece
Oct 02, 2012, 1:48 PM
I'm not doing anything client side, I have no validation javascript side, if you run the example you will see the problem. On the direct event in the post event it sends up the value ".." for the number field which hits LoadPostData in Field.cs (Ext code) and when it hits CheckRange in NumberFieldBase.cs it throws an error when trying to parse ".." to a double.

When you leave the number field on the client side it still shows as invalid, but since I have no validation client side as soon as anything hits the server it breaks because the Ext code fails to parse the string version of ".." which breaks all direct methods and events.

Vladimir
Oct 02, 2012, 2:00 PM
Ok, clear
We will make default behaviour as in DateField

Vladimir
Oct 05, 2012, 6:20 PM
Please update from SVN and retest

JulianPreece
Oct 08, 2012, 11:42 AM
Please update from SVN and retest

It works, but a couple of problems I have noticed:

1. by entering a . as the value and then firing a direct it method replaces the control value with "-1.7976931348623157e+308" (double.MinValue), should this not update the control value client side? - not a major issue, by setting EmptyValue to 0 its fine anyway.
2. setting EmptyValue from markup causes the error : "Cannot create an object of type 'System.Object' from its string representation '0' for the 'EmptyValue' property.", setting it from code behind works but any value that isn't a double causes it to throw an error as it hits "Convert.ToDouble(this.EmptyValue)", could it be set as a double instead of an object for the property?

But I can work with it now to handle values server side so thanks!

Vladimir
Oct 08, 2012, 11:48 AM
1. by entering a . as the value and then firing a direct it method replaces the control value with "-1.7976931348623157e+308" (double.MinValue), should this not update the control value client side? - not a major issue, by setting EmptyValue to 0 its fine anyway.

The same behaviour like in DateField. If parsing is failed then EmptyValue is used



2. setting EmptyValue from markup causes the error : "Cannot create an object of type 'System.Object' from its string representation '0' for the 'EmptyValue' property.", setting it from code behind works but any value that isn't a double causes it to throw an error as it hits "Convert.ToDouble(this.EmptyValue)", could it be set as a double instead of an object for the property?

EmptyValue is defined in base class for Field widgets. Therefore type is object (widgets can use different types). ASP.NET doesn't support to set object types via markup. And for NumberField you have to use approperiate type (double) for EmptyValue because number field uses double only for a value

JulianPreece
Oct 08, 2012, 12:04 PM
The same behaviour like in DateField. If parsing is failed then EmptyValue is used

If you put a random string in a date field the value server side is DateTime.MinValue, but client side it stays the same, it doesnt select the minum date, so its not the same behaviour.


EmptyValue is defined in base class for Field widgets. Therefore type is object (widgets can use different types). ASP.NET doesn't support to set object types via markup. And for NumberField you have to use approperiate type (double) for EmptyValue because number field uses double only for a value

I see, I thought it might be the case since Field declares it as an object.

A thought, couldn't EmptyValue be declared as:
public new double EmptyValue
instead of:
public override object EmptyValue

and cast the get return type to double, this could apply to the other fields as well so we can set it from markup which is much more manageable?

Daniil
Oct 09, 2012, 6:08 PM
Corrected in SVN. Thank you for the report!