Every now and then I will have a server control that I need to include a value generated from code inside a field on the control. For example:
<asp:label id="myname" runat="server" text="<% CurrentUserName %>" />
That will shoot out the all too common ASP.NET "server tags cannot contain <% ...%>" error message.
I found this article on using a custom ExpressionBuilder to make it easy to access data fetched from code inside these fields:
http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspxThe solution from the article is a simple class:
[ExpressionPrefix("Code")]public class CodeExpressionBuilder : ExpressionBuilder
{
public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context)
{
return new CodeSnippetExpression(entry.Expression);
}
}
and map that into your web.config file under ExpressionBuilders. After that you can use code such as:
<asp:label id="myname" runat="server" text="<% Code: CurrentUserName %>" />
Pretty slick! Be sure to check the article for more information on the process!