Formatting EVAL expressions in Asp.Net

This is a quick post on examples on the use of EVAL expression to render special formatting on DataBound Controls in Asp.Net webforms. With Asp.Net DataBinding, developers sometimes require binding data returned back from SQL Stored Procedure or SQL Function to be represented on webforms in a certain way, which is more readable and user friendly.

The Syntax for EVAL is <%# Eval(“ColumnName”)%>

Example: Date Formatting

Project Started: <%#Eval(“ProjectStartDate”, “{0:dd, MMM yyyy}”)%>
You want to show ’12, December 2012′ instead of 2012-12-12 13:30:00
Project Started: <%#Eval(“ProjectStartDate”, “{0: HH:mm}”)%>
You want to show ’13:30′ instead of 2012-12-12 13:30:00
* use the Tools section to learn more about the Asp.Net Date Time String Formats

Example: Yes/No Boolean Type Column

Likes Asp.Net: <%#(Eval(“LikeAsp”).Equals(true)? “Yes”:”No”)%>
You want to show ‘Yes’ and ‘No’ instead of 1 / 0

Example: String Formatting

Name: <%#String.Concat(Eval(“Roger”),”-Moore 007″)%>
You want to show Roger-Moore 007 instead of Roger

Example: IIf / If Condition : Change visibility of control

Link: <asp:HyperLink ID=”LinkForManager” runat=”server” Text=”Manager” Visible='<%# IIf(Eval(“AnyColumn”) Is DbNull.Value, “False”,”True”) %>’  NavigateUrl=”~/link.aspx”/>
You want to show Manager Link only when Bit Column is True / 1

Example: IIf / If Condition: Show Text based on Boolean

Gender: <%#IIf(Eval(“CandidateGender”).Equals(True), “Male”, “Female”)%>
You want to show ‘Male’ or ‘Female’ on CandidateGender Boolean column

* updated 10 Jan 2013

Example: Visibility of Control based on Bit / Boolean Column Value
<asp:Button ID=”Button1″ runat=”server” Text=”Button1″ Visible='<%#Convert.ToBoolean(Eval(“BitColumn”))%>’ />
Where 0 is button1.visible = false and 1 is button1.visible = true

I will keep updating this post if I discover some examples in future.
The above can also be used with BIND expression as well.

Further Reference:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s