
Working with Resources in Web Pages
After you create resource files, you can use them in ASP.NET Web pages. You typically use resources to fill the property values of controls on the page. For example, you might use resources to set the Text property of a Button control, instead of hard-coding the property to a specific string.
To use resources to set control property values, you can use implicit localization or explicit localization, as follows:
Implicit localization works with local resources and lets you automatically set control properties to matching resources.
Explicit localization lets you use a resource expression to set a control property to a specific resource in a local or global resource file.
Implicit Localization with Local Resources
If you have created local resource files for a specific page, you can use implicit localization to fill the property values for a control from the resource file. In implicit localization, ASP.NET reads a resource file and matches resources to property values.
To use implicit localization, you must use a naming convention for resources in the local resource file that uses the following pattern:
Key.Property
For example, if you are creating resources for a Button control named Button1, you might create the following key/value pairs in the local resource file:
|
Button1.Text
Button1.BackColor
Label1.Text
|
You can use any name for Key, but Property must match the name of a property of the control that you are localizing.
In the page, you use a special meta attribute in the markup for the control to specify implicit localization. You do not have to explicitly specify which properties are localized. A Button control that is configured for implicit localization might resemble the following:
|
<asp:Button ID="Button1" runat="server" Text="DefaultText"
meta:resourcekey="Button1" />
|
The resourcekey value matches a key in the corresponding resource file. At run time, ASP.NET matches resources to control properties using the control label as the resourcekey. If a property value is defined in the resource file, ASP.NET substitutes the resource value for the property.
Explicit Localization
Alternatively, you can use explicit localization, where you use a resource expression. Unlike implicit localization, you must use a resource expression for each property that you want to set.
A Button control that is configured to set the Text property from a global resource file might resemble the following:
|
<asp:Button ID="Button1" runat="server"
Text="<%$ Resources:WebResources, Button1Caption %>" />
|
The resource expression takes the following form, where Class is optional, unless the resource is a global one, and ResourceID is required:
<%$Resources:Class,ResourceID%>
The Class value identifies the resource file to use when you use global resources. When .resx files are compiled, the base file name, without extensions, is used as the class name of the resulting assembly, explicitly. If you want to use resources from a local resource file (one that matches the current page name), you do not have to include a class name. This is because ASP.NET matches the page class to the resource class.
The ResourceID value is the identifier of the resource to read. In the previous example, the Text property for the button is read from the global resource file WebResources.resx (or the appropriate localized version). In that file, ASP.NET uses the value for the resource with the identifier Button1Caption and for the page itself. To set page properties, you can use resource expressions in the @ Page directive.
You can specify an explicit resource expression or an implicit resource expression for a control, but not both. The following declarative syntax on a Button control causes a parser error:
|
<asp:Button ID="Button1"
runat="server"
meta:resourcekey="Button1Resource1"
Text="<%$ Resources:WebResources, Button1Caption %>" />
|
In this example, an implicit local resource file (one that matches the current page name) is specified as well as an explicit resource file that is named WebResources. To prevent a parser error for this control, remove one of the resource expressions.
Localizing Static Text
If a page includes static text, you can use ASP.NET localization by including it in a Localize control, and then using explicit localization to set the static text. The Localize control renders no markup; its only function is to act as a placeholder for localized text. The Localize control can be edited in Design view, not only in the property grid. At run time, ASP.NET treats the Localize control as a Literal control. For example, your page might include the following code:
|
<h1>
<asp:Localize runat=server
ID="WelcomeMessage"
Text="Welcome!" meta:resourcekey="LiteralResource1" />
</h1>
<br />
<br />
<asp:Localize runat="server"
ID="NameCaption"
Text="Name: " meta:resourcekey="LiteralResource2" />
<asp:TextBox runat="server" ID="TextBox1"
meta:resourcekey="TextBox1Resource1" />
|
Security Note: |
|---|
This example has a text box that accepts user input, which is a potential security threat. By default, ASP.NET Web pages validate that user input does not include script or HTML elements. For more information, see
Script Exploits Overview.
|
Implicit Localization in Templates
In templated controls, such as the DataList, GridView, and Wizard controls, you localize template style properties by accessing the properties through the parent control's implicit resource expression. You cannot use an implicit resource expression on the template itself.
To localize values for a template property, use the meta attribute and a resource key for the control that the template belongs to. Then use either the Property.Subproperty syntax or the Property-Subproperty syntax in the resource file. For example, the following declarative syntax is valid for a Wizard control:
|
<asp:Wizard ID="Wizard1"
runat="server"
meta:resourcekey="Wizard1Resource1">
<NavigationStyle
BorderWidth="<%$ resources:navBorderWidth %>"/>
<WizardSteps>
<asp:WizardStep ID="WizardStep1"
runat="server"
Title="Step 1"
meta:resourcekey="WizardStep1Resource1">
</asp:WizardStep>
</WizardSteps>
</asp:Wizard>
|
The following key/value pairs in the local resource file could be used for the previous example:
|
Wizard1Resource1.NavigationStyle.BackColor, Red
navborderWidth, 5
|
Or the following key/value pairs could be used:
|
Wizard1Resource1.NavigationStyle-BackColor, Red
navborderWidth, 5
|
You can use an explicit resource expression for the NavigationStyle property of the Wizard control in the previous example. The explicit resource expression omits the Class name so that resources from a local resource file are used.
For more information about templated server controls, see ASP.NET Web Server Controls Templates.