Showing posts with label data binding. Show all posts
Showing posts with label data binding. Show all posts

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; }
}
}

Friday, December 7, 2007

Limited formatting in DataBinder.Eval

I've been requested to display special text ("n.a.") in the case that evaluated expression is not there (is null). I've intended to provide my custom format provider that would work like requested. Unfortunately, DataBinder.Eval does not allow you to provide your custom format provider even though string.Format is called internally that can handle it.

I've solved it by creating my custom control inherited from Label that provides special text when the Text property is null or empty on rendering. I would consider the ability to provide my custom formatter more convenient and somewhat smarter, however.