Welcome to Shaun Luttin's public notebook. It contains rough, practical notes. The guiding idea is that, despite what marketing tells us, there are no experts at anything. Sharing our half-baked ideas helps everyone. We're all just muddling thru. Find out more about our work at bigfont.ca.

Inline Expressions in ASP.NET Web Forms

Tags: asp.net, .net, web-forms, legacy

<%= ... %> displaying expression

  • display the result of an expression
  • will not work in a server control attribute
  • <%= DateTime.Now() %>

<%# ... %> data-binding expression

  • bind a server control property to a data source...
  • ...when the control's DataBind method is called
  • Three techniques:

    1. Call Eval() for one-way, read-only binding
    2. Call Bind() for two-way, read-write binding
    3. Call any publicly available code and and display its return value.
  • <%# MyMethodOrProperty() %>

<%$ ... %> expression builder

  • set values based on configuration or resource files
  • e.g. read web.config settings
  • <%$ AppSettings: someKey %>

<%-- ... -- %> server-side comments block

  • comment out code.
  • <%-- This is a comment. --%>

<%@ ... %> directive expression

  • provide page and user control settings
  • <%@Page Language="C#" ... />

<% ... %> embedded code blocks

  • designed for backward compatibility
  • avoid

See Also

Introduction to ASP.NET inline expressions in the .NET Framework