Brian's Blog

items I see across my tribes

Easy Fix for "server tags cannot contain <% ...%>

November 09
by briancarter 9. November 2008 22:56

I'm always trying to jack up controls with shortcuts like:

<asp:Label id="Label1" runat="server" Text="<%= CurrentUserName %>" /> 

You could use "#" instead of the "=", but that requires a databind. 

A new feature in ASP.NET 2.0 to handle this is Expression Builders. Expression builders allow for some pretty interesting interaction with the ASP.NET compilation model.

For example, new to ASP.NET 2.0 is the ability to reference appSettings declaratively. Lets say you wanted the text of a button to be based on a value in the appSettings section of your web.config. Piece of cake:

<asp:Button id="cmdSubmit" runat="server" Text="<%$ appSettings: ButtonText %>" />

This is made possible by the built in AppSettingsExpressionBuilder. Lets say you wanted to display a localizable string, which is stored as a resource. In ASP.NET 1.1 it was sort of a pain. No longer:

<%$ resources: ResourceKey %>

That's the ResourceExpressionBuilder hard at work. Nice!

There's also a ConnectionStringExpressionBuilder so you can refer to Connection Strings defined in the new connection strings section in the web.config. That is extremely helpful when working with the new declarative data controls:

<asp:SqlDataSource id="data1" runat="server" ConnectionString="<%$ ConnectionStrings: MyConnectionString %>"/>

Pretty useful.  How about writing your one to do something like:

<asp:CheckBox id="chk1" runat="server" Text="<%$ Code: DateTime.Now %>"/> 

Get the code and more details at:

http://weblogs.asp.net/infinitiesloop/archive/2006/08/09/The-CodeExpressionBuilder.aspx

A great example: http://www.hintsandtips.com/ShowPost/456/hat.aspx

Categories: Development

Comments

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading




 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.