我的asp.net应用程序有多个文本框,用于货币输入。所以我以编程方式将比较验证器添加到应用程序中。我必须只允许美国货币格式,然后我使用Thread文化信息并且工作正常。但Page文化信息不得影响这种文化信息。例如;当用户从美国输入时,页面标签和验证错误必须以英语显示,并且当用户从土耳其输入时,页面语言必须以土耳其语显示。但是当我将Thread文化信息提供给我的比较验证器类时,比较验证器货币格式是US(这很好),验证错误也是英语(这不是很好,必须显示为页面语言)。如何分隔货币文化信息和错误消息文化信息?我的代码:
Validator.cs
public class Validator
{
public Validator()
{
CultureInfo Cul = CultureInfo.CreateSpecificCulture("en-US");
System.Threading.Thread.CurrentThread.CurrentUICulture = Cul;
System.Threading.Thread.CurrentThread.CurrentCulture = Cul;
}
public CompareValidator CreateCompareValidator(string id, string validationGroup, string controlToValidate, bool enableClientScript, string errorMessage)
{
CompareValidator compareValidator = new CompareValidator();
compareValidator.ID = id;
compareValidator.ValidationGroup = validationGroup;
compareValidator.ControlToValidate = controlToValidate;
compareValidator.Operator = ValidationCompareOperator.DataTypeCheck;
compareValidator.Type = ValidationDataType.Currency;
compareValidator.Display = ValidatorDisplay.None;
compareValidator.EnableClientScript = enableClientScript;
compareValidator.ErrorMessage = errorMessage;
compareValidator.CultureInvariantValues = true;
return compareValidator;
}
}
name.aspx.cs
private void ValidateCurrencyTextBox()
{
AddValidator(txtBaTutar.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtBsTutar.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtBpbBakiye1.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtBpbBakiye2.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtBpbBakiye3.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtBpbBakiye4.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtBpbBakiye5.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtTRYBakiye1.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtTRYBakiye2.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtTRYBakiye3.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtTRYBakiye4.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtTRYBakiye5.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtVergiMatrahi.ID, GetLocalResourceObject("bsWrongAmount").ToString());
AddValidator(txtVergiTutari.ID, GetLocalResourceObject("bsWrongAmount").ToString());
}
private void AddValidator(string controlToValidate, string errorMessage)
{
Validator validator = new Validator();
CompareValidator cmp = validator.CreateCompareValidator("cmpv" + controlToValidate, "errors", controlToValidate, false, errorMessage);
Page.Form.Controls.Add(cmp);
}
答案 0 :(得分:1)
尝试仅使用CurrentCulture作为货币,并使用CurrentUICulture作为页面语言。