What’s the difference between <%= %> and <%# %>

A daily question from a while back that had perplexed me for ages:

Q: In a DataBound control, what’s the difference between using a <%= %> block, and a <%# %> block?

Of course I actually did know the difference, and when I should use each one, but Bender summed up the correct answer the best:

A: You use a <%# %> block if you need access to the underlying DataSource of the repeating control (e.g. you need to access the DataItem with an Eval statement, or you need access to the Container element).

When you do not need to access the underlying DataSource, use a <%= %> block instead.

Posted in .Net, Daily Question by Gerrod at November 20th, 2006.

Comments are closed.