Tuesday, February 12, 2008

Limited Formatting in Bind

You can specify not only what property you are binding to, but also the format that should be used. Example:
<asp:TextBox Text='<% Bind("SomeProperty", "{0:N2}") %>' />
to get number with two decimal digits.

The problem is that the format is applied in one-way manner. When the number is loaded, it is formatted perfectly. But the very same number (without editing) can be stored no more. (It depends on culture. I have discovered this when using non-us decimal separators.)

WebNumericEdit and Nullable<decimal>

I was binding Infragistics WebNumericEdit to a Nullable<decimal>. I had to set DataMode of control to NumericDataMode.Decimal. I set Nullable property to true since the value may be missing. (That is also the reason why we store the value as Nullable<decimal>.) It seemed working, but then I noticed that 0 is propagated to data instead of null. So it seems that Nullable<decimal> is not directly supported by WebNumericEdit.

After the short brainstorming, the decision was made to try to override the way how data are provided. Finally, this was sufficient to do:


public class WebNullableNumericEdit
: WebNumericEdit
{
public override object Value
{
get
{
return (this.Nullable &&
this.Text.Equals(this.NullText)) ?
null : base.Value;
}
set { base.Value = value; }
}
}