Code Renaissance is about building great teams and great software. By exploring best practices, team interactions, design, testing and related skills Code Renaissance strives to help you create the team and codebase that you've always wanted.

ASP.Net Code Blocks: Pound vs Equal (%# vs %=)

I got a runtime error after changing a pound code block(<%#statement%>) to equals code block( <%=statement> ) in some legacy ASP.Net code that I was cleaning up . I'd never seen the pound version before and had assumed it was an old obsolete way of doing things. Since this clearly wasn't the case I decided to do a bit of digging.

Eventually I turned up a post on code blocks by Dan Crevier that shed some light on things.

He summarizes the differences as follows:

  • The <%= (equal) expressions are evaluated at render time.
  • The <%# (pound) expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
  • <%# (pound) expressions can be used as properties in server-side controls. <%= (equal) expressions cannot.

This helped explain the error I got; unfortunately the post itself was hard for me to find (Google doesn't support searching for non-alphanumeric characters like less-than, percent, pound and equal) so I'm throwing this out there in hopes that it'll show better in search and help some people. All props to Dan though; he did a lot of work to research this.

Thanks Dan.

3 - What do you think?:

JBL said...

Thanks for posting this. It answered the exact question I was trying to search for.

DH said...

This came out of an extended period of google gymnastics where I just couldn't find an answer using the terms I was searching for. Glad I could help make the answer findable/googlable.

JD said...

Thank you! I'm glad you made a title that is actually searchable on google as this was a pain to find.