C# 如何从资源文件在CustomValidator上加载ErrorMessage?
我想从资源文件中为我的C# 如何从资源文件在CustomValidator上加载ErrorMessage?,c#,asp.net,globalization,C#,Asp.net,Globalization,我想从资源文件中为我的CustomValidator加载ErrorMessage 我的CustomValidator设置如下: <asp:CustomValidator ID="cv1" runat="server" ControlToValidate="txt1" ErrorMessage="TEXT TO BE LOCALIZED" OnServerValidate="cv1_Validate" /> 验证工作正常,但我希望从本地资源文件中提取ErrorMes
CustomValidator
加载ErrorMessage
我的CustomValidator
设置如下:
<asp:CustomValidator ID="cv1" runat="server" ControlToValidate="txt1"
ErrorMessage="TEXT TO BE LOCALIZED" OnServerValidate="cv1_Validate" />
验证工作正常,但我希望从本地资源文件中提取ErrorMessage
编辑:我还想知道是否有任何方法可以使用
meta:resourcekey
实现这一点,假设您的页面(或控件)有本地资源,这就是实现这一点的方法
ErrorMessage="<%$ resources:ResourceName %>"
ErrorMessage=“”
如果您从全局资源文件中获取文本,您应该执行以下操作
ErrorMessage="<%$ resources:Strings, ResourceName %>"
ErrorMessage=“”
其中Strings
是文件名。这种方法称为显式本地化
编辑
您可以使用meta:resourcekey
。这称为隐式本地化。您需要有本地资源,因为这种方法对全局资源无效
- 它们位于App\u LocalResources文件夹中
- 基本名称与页面名称匹配
- 文件中的资源使用命名约定resourcekey.“属性”
如果要在代码隐藏中执行此操作,可以使用以下命令:
ResourceManager resmgr = new ResourceManager("YourApplication.YourBaseResourceFile ",
Assembly.GetExecutingAssembly());
protected void cv1_Validate(object source, ServerValidateEventArgs e)
{
if (FalseCondition)
{
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
String str = resmgr.GetString("Error Msg Key in Resource File");
cv1.ErrorMessage =str;
e.IsValid = false;
}
else
{
e.IsValid = true;
}
}
ResourceManager resmgr = new ResourceManager("YourApplication.YourBaseResourceFile ",
Assembly.GetExecutingAssembly());
protected void cv1_Validate(object source, ServerValidateEventArgs e)
{
if (FalseCondition)
{
CultureInfo ci = Thread.CurrentThread.CurrentCulture;
String str = resmgr.GetString("Error Msg Key in Resource File");
cv1.ErrorMessage =str;
e.IsValid = false;
}
else
{
e.IsValid = true;
}
}