How to localize a BoundField / TemplateField HeaderText
In order to localize a HeaderText property, you can use the common:
HeaderText='<%= MyResources.str_ResourceName1; %>'
This code will ONLY work if you store your resources in your local web project resource file. If you store your resources in a sattelite assembly, then this code will not be interpreted. Because the HeaderText property is not parsed by the ASP.NET built-in expression engine when referring to a sattelite resource assembly, this text will be rendered as is without being evaluated as a code expression.
Luckily, ASP.NET provides a way for you to define your own custom expression handler to parse this code expression and replace it with the value of the code block. You can build your own expression parser by inheriting from the ExpressionBuilder class.
You may have used this class in the past because there are a few built-in ExpressionBuilders in .NET including:
Gets a connection string by name
<%$ connectionStrings:MyConnectionStringName %>
Gets a setting in the appSettings section of the Web.config file by name
<%$ appSettings:MyAppSettingName %>
Gets a resource string ONLY if it is in the web project
<%$ resources:MyResourceKeyName %>
Create a new class in the App_Code directory to use the name of your resource object with "ExpressionBuilder" on the end. In my case, my resource assembly is named "MyResources" so I named this object "MyResourcesExpressionBuilder".
Add the following methods to the class:
// This class inherits from the .NET ExpressionBuilder object. The methods below are // overrides required to implement your own custom expression parsing logic.public class MyResourcesExpressionBuilder : ExpressionBuilder
{ // Retrieves the value you want to return as an object based on the expression specified in // the code block you specified in the '<%$ MyExpression goes here %>' property.public static object GetEvalData(string expression, Type target, string entry)
{ // get a reference to the assembly that contains my resource strings. NOTE: my // assembly name is: MyResources.dll System.Reflection.Assembly assembly = System.Reflection.Assembly.Load("MyResources"); // Load the type from the assembly that contains my resource strings. NOTE: my // type is in the MyResources namespace and is called MyResourceObjectName. Type type = assembly.GetType("MyResources.MyResourceObjectName"); // create a new instance of the resource manager class using the type. System.Resources.ResourceManager resMgr = new System.Resources.ResourceManager(type); // get the resource string which is just the string name specified in the expression. return resMgr.GetString(expression);}
// This method is called when an expression is encountered by the parser. It calls our custom method // to evaluate the expression.public override object EvaluateExpression(object target, BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{ return GetEvalData(entry.Expression, target.GetType(), entry.Name);}
// This method parses the expression encountered in the '<%$ MyExpression goes here %>' property.public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{Type type1 = entry.DeclaringType;
PropertyDescriptor descriptor1 = TypeDescriptor.GetProperties(type1)[entry.PropertyInfo.Name];
CodeExpression[] expressionArray1 = new CodeExpression[3]; expressionArray1[0] = new CodePrimitiveExpression(entry.Expression.Trim()); expressionArray1[1] = new CodeTypeOfExpression(type1); expressionArray1[2] = new CodePrimitiveExpression(entry.Name);return new CodeCastExpression(descriptor1.PropertyType, new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(base.GetType()), "GetEvalData", expressionArray1));
}
// Specifies that this class supports the ability to evaluate an expression.public override bool SupportsEvaluate
{ get { return true; }}
}
Now you need to add a section to your Web.config file to register this new expression builder so that it can be used in your ASP.NET pages. Look for the node section:
<configuration>
<system.web>
<compilation debug="true">
You should see an
<expressionBuilders>
<add expressionPrefix="MyResourcesExpression" type="MyResourcesExpressionBuilder"/>
</expressionBuilders>
Set the expressionPrefix property to the name you want to use (something unique!) as the term that triggers the system to evaluate your custom expression when the interpreter encounters it. Set the type property to the name of your custom ExpressionBuilder class you just created.
Finally, in your GridView, change the HeaderText property of a BoundField or TemplateField to the following:
<asp:BoundField DataField='MyField' HeaderText='<%$MyResourcesExpression:str_NameOfStringResource %>' />
Note the use of the "$" sign to tell ASP.NET to parse the text in the property as an expression. The format of the expression is to use the [Expression prefix]:[string resource object name]. My expression prefix is MyResourcesExpression and my resource in my .resx file is named "str_NameOfStringResource".
Now when you compile and run this page, the bound field's header text property will be set to the value of the str_NameOfStringResource field and correctly displayed.
Popular Articles
Last viewed:
- How to localize a BoundField / TemplateField HeaderText
- How to Highlight the Day in the ASP.NET Calendar Control with the SelectedDate Property
- ObjectDataSource TypeName with Constructor Parameters
- Building a Web Service in ASP.NET 3.5
- C# Free Component to Generate PDF - Convert HTML to PDF
- Disable Button on Submit (Prevent multiple submits)

Recent comments
21 hours 17 min ago
4 days 12 hours ago
1 week 2 days ago
1 week 6 days ago
2 weeks 1 day ago
2 weeks 5 days ago
2 weeks 6 days ago
2 weeks 6 days ago
3 weeks 4 days ago
4 weeks 1 day ago