Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET string resource to lower case?

I'm including a string resource in an ASPX:

<asp:Literal runat="server" Text="<%$ Resources:Global, MyString %>"/>

Let's say the value of MyString is "Home". How can I convert that to lower case ("home") in the resource tag? E.g., I don't want to have to store both upper/title and lower case variants of the string in the resource file.

I realize I could do this normally (outside a control) like this:

<%= Resources.Global.MyString.ToLower() %>

But that doesn't help when I have to use a resource for some property of a control. I was hoping to be able to do something simple such as:

<asp:Literal runat="server" Text="<%$ (Resources:Global, MyString).ToLower() %>"/>
like image 729
Josh M. Avatar asked Nov 25 '25 05:11

Josh M.


1 Answers

I ended up building my own ExpressionBuilder which uses the built-in ResourceExpressionBuilder to get the underlying resource value and then convert it to lower case:

Convert the Base Expression to a Lower-case String

public class ResourceLowerCase : ResourceExpressionBuilder
{
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
    {
        CodeExpression getResourceExpression = base.GetCodeExpression(entry, parsedData, context);
        CodeMethodInvokeExpression toStringExpression = new CodeMethodInvokeExpression(getResourceExpression, "ToString");
        CodeMethodInvokeExpression toLowerExpression = new CodeMethodInvokeExpression(toStringExpression, "ToLower");

        return toLowerExpression;
    }
}

Register the Expression Builder

<system.web>
    <expressionBuilders>
        <add expressionPrefix="ResourceLowerCase" type="My.Project.Compilation.ResourceLowerCase"/>
    </expressionBuilders>
</compilation>

Invoke the Expression Builder

<asp:Literal runat="server" Text="<%$ ResourceLowerCase:Global, MyString %>" />
like image 83
Josh M. Avatar answered Nov 27 '25 20:11

Josh M.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!