Wednesday, June 15, 2011

How to use ASP.NET Expression to set property in server control?

ASP.NET expressions are a declarative way set control properties based on information that is evaluated at run time.
<asp:Label id="label1" runat="server" text="<%$ Resources: Messages, ThankYouLabel %>" />
<asp:Literal runat="server" text="<%$ AppSettings: copyright %>"/>

ASP.NET Expression Builders

public class CodeExpressionBuilder : ExpressionBuilder {
    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, 
       object parsedData, ExpressionBuilderContext context) {
        return new CodeSnippetExpression(entry.Expression);
<compilation debug="true">
    <add expressionPrefix="Code" type="Infinity.Web.Compilation.CodeExpressionBuilder"/>
And to see it in action:
<asp:CheckBox id="chk1" runat="server" Text="<%$ Code: DateTime.Now %>" />

LINQ Cheat Sheet

Microsoft patches 34 vulnerabilities