我想从资源文件加载我的CustomValidator的ErrorMessage.
我的CustomValidator设置如下:
<asp:CustomValidator ID="cv1" runat="server" ControlToValidate="txt1" ErrorMessage="TEXT TO BE LOCALIZED" OnServerValidate="cv1_Validate" />
我的验证方法如下:
protected void cv1_Validate(object source,ServerValidateEventArgs e) { if (FalseCondition) { e.IsValid = false; } else { e.IsValid = true; } }
验证工作正常,但我想从我的本地资源文件中提取ErrorMessage.
编辑:我也很好奇有没有办法使用Meta:resourcekey这样做.
解决方法
假设您有一个页面(或控件)的本地资源,这将是这样做的方法
ErrorMessage="<%$resources:ResourceName %>"
ErrorMessage="<%$resources:Strings,ResourceName %>"
编辑
您可以使用Meta:resourcekey.这称为隐式本地化.您需要拥有本地资源,因为此方法对全局资源无效.
Make sure that you have local resource files (.resx files) that meet the following criteria:
They are in an App_LocalResources folder.
The base name matches the page name.
For example,if you are working with the page named Default.aspx,the resource files are named Default.aspx.resx (for the default resources),Default.aspx.es.resx,Default.aspx.es-mx.resx,and so on.
- The resources in the file use the naming convention resourcekey.”property”.
For example,key name Button1.”Text”.
In the control markup,add an implicit localization attribute.
For example:
<asp:Button ID=”Button1″ runat=”server” Text=”DefaultText”
Meta:resourcekey=”Button1″ />
资料来源:MSDN