I have created a itemplate for edit numeric data on a gridview. Everything is ok except databinding. The template has one textbox, one AjaxControlToolkit.MaskedEditExtender and one AjaxControlToolkit.MaskedEditValidator. The mask for the extender is "999,999,999.99", when the databinding is executed if the value to bind as more or less decimal numbers it show worng on de grid.
Examples:
value to bind = 1.5
on the grid = 0.15
expected = 1.50
value to bind = 2
on the grid = 0.2
expected = 2.00
value to bind = 158487.5878
on the grid = 15,848,758.78
expected = 158,487.59
Example source code:
public struct BetterSoftColumnConfigStruct
{
public string FieldName;
public string HeaderText;
public int ColumnSize;
public string DataFormatString;
public string DataType;
public bool Visible;
public bool ReadOnly;
}
private class MyTemplate : ITemplate
{
protected TextBox txbNumeric;
protected AjaxControlToolkit.MaskedEditExtender meeNumeric;
protected AjaxControlToolkit.MaskedEditValidator mevNumeric;
private BetterSoftColumnConfigStruct bsccCfg;
public MyTemplate(BetterSoftColumnConfigStruct pCfg)
{
bsccCfg = pCfg;
}
public void InstantiateIn(System.Web.UI.Control ctrlContainer)
{
txbNumeric = new TextBox();
txbNumeric.ID = bsccCfg.FieldName;
txbNumeric.DataBinding += new EventHandler(txbNumeric_DataBinding);
meeNumeric = new AjaxControlToolkit.MaskedEditExtender();
meeNumeric.ID = txbNumeric.ID + "meeNumeric";
meeNumeric.TargetControlID = bsccCfg.FieldName;
meeNumeric.MessageValidatorTip = true;
meeNumeric.OnFocusCssClass = "MaskedEditFocus";
meeNumeric.OnInvalidCssClass = "MaskedEditError";
meeNumeric.MaskType = AjaxControlToolkit.MaskedEditType.Number;
meeNumeric.Mask = "999,999,999.99";
meeNumeric.ClearMaskOnLostFocus = true;
meeNumeric.InputDirection = AjaxControlToolkit.MaskedEditInputDirection.RightToLeft;
meeNumeric.DisplayMoney = AjaxControlToolkit.MaskedEditShowSymbol.None;
meeNumeric.AcceptNegative = AjaxControlToolkit.MaskedEditShowSymbol.Left;
meeNumeric.CultureName = System.Globalization.CultureInfo.CurrentCulture.Name;
meeNumeric.ErrorTooltipEnabled = true;
mevNumeric = new AjaxControlToolkit.MaskedEditValidator();
mevNumeric.ID = txbNumeric.ID + "mevNumeric";
mevNumeric.ControlExtender = meeNumeric.ID;
mevNumeric.ControlToValidate = txbNumeric.ID;
mevNumeric.EmptyValueMessage = "*";
mevNumeric.InvalidValueMessage = "*";
mevNumeric.Display = ValidatorDisplay.Dynamic;
mevNumeric.ToolTip = "*";
mevNumeric.EmptyValueBlurredText = "*";
mevNumeric.InvalidValueBlurredMessage = "*";
ctrlContainer.Controls.Add(txbNumeric);
ctrlContainer.Controls.Add(meeNumeric);
ctrlContainer.Controls.Add(mevNumeric);
}
void txbNumeric_DataBinding(object sender, EventArgs e)
{
TextBox txbBox = (TextBox)sender;
GridDataItem gdiContainer = (GridDataItem)txbBox.NamingContainer;
double i = Convert.ToDouble(((DataRowView)gdiContainer.DataItem)[bsccCfg.FieldName]);
txbBox.Text = i.ToString();
}
}
Comments: Workaround found, see comments.
Examples:
value to bind = 1.5
on the grid = 0.15
expected = 1.50
value to bind = 2
on the grid = 0.2
expected = 2.00
value to bind = 158487.5878
on the grid = 15,848,758.78
expected = 158,487.59
Example source code:
public struct BetterSoftColumnConfigStruct
{
public string FieldName;
public string HeaderText;
public int ColumnSize;
public string DataFormatString;
public string DataType;
public bool Visible;
public bool ReadOnly;
}
private class MyTemplate : ITemplate
{
protected TextBox txbNumeric;
protected AjaxControlToolkit.MaskedEditExtender meeNumeric;
protected AjaxControlToolkit.MaskedEditValidator mevNumeric;
private BetterSoftColumnConfigStruct bsccCfg;
public MyTemplate(BetterSoftColumnConfigStruct pCfg)
{
bsccCfg = pCfg;
}
public void InstantiateIn(System.Web.UI.Control ctrlContainer)
{
txbNumeric = new TextBox();
txbNumeric.ID = bsccCfg.FieldName;
txbNumeric.DataBinding += new EventHandler(txbNumeric_DataBinding);
meeNumeric = new AjaxControlToolkit.MaskedEditExtender();
meeNumeric.ID = txbNumeric.ID + "meeNumeric";
meeNumeric.TargetControlID = bsccCfg.FieldName;
meeNumeric.MessageValidatorTip = true;
meeNumeric.OnFocusCssClass = "MaskedEditFocus";
meeNumeric.OnInvalidCssClass = "MaskedEditError";
meeNumeric.MaskType = AjaxControlToolkit.MaskedEditType.Number;
meeNumeric.Mask = "999,999,999.99";
meeNumeric.ClearMaskOnLostFocus = true;
meeNumeric.InputDirection = AjaxControlToolkit.MaskedEditInputDirection.RightToLeft;
meeNumeric.DisplayMoney = AjaxControlToolkit.MaskedEditShowSymbol.None;
meeNumeric.AcceptNegative = AjaxControlToolkit.MaskedEditShowSymbol.Left;
meeNumeric.CultureName = System.Globalization.CultureInfo.CurrentCulture.Name;
meeNumeric.ErrorTooltipEnabled = true;
mevNumeric = new AjaxControlToolkit.MaskedEditValidator();
mevNumeric.ID = txbNumeric.ID + "mevNumeric";
mevNumeric.ControlExtender = meeNumeric.ID;
mevNumeric.ControlToValidate = txbNumeric.ID;
mevNumeric.EmptyValueMessage = "*";
mevNumeric.InvalidValueMessage = "*";
mevNumeric.Display = ValidatorDisplay.Dynamic;
mevNumeric.ToolTip = "*";
mevNumeric.EmptyValueBlurredText = "*";
mevNumeric.InvalidValueBlurredMessage = "*";
ctrlContainer.Controls.Add(txbNumeric);
ctrlContainer.Controls.Add(meeNumeric);
ctrlContainer.Controls.Add(mevNumeric);
}
void txbNumeric_DataBinding(object sender, EventArgs e)
{
TextBox txbBox = (TextBox)sender;
GridDataItem gdiContainer = (GridDataItem)txbBox.NamingContainer;
double i = Convert.ToDouble(((DataRowView)gdiContainer.DataItem)[bsccCfg.FieldName]);
txbBox.Text = i.ToString();
}
}
Comments: Workaround found, see comments.