Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

inline conditional statement on Eval

Status
Not open for further replies.

stinkybee

Programmer
May 15, 2001
218
GB
I am trying to test a dataitem for an empty string and display "N/A" if the string is empty, otherwise display the string. For example, I have tried this:

Code:
<%# Eval("code").ToString()=="" ? "N/A" : Eval("code") %>

As suggested by numerous web searches. However, this does not work and produces an "Expression expected." error

Any ideas?

Web Development Manager
 
Here is an example that I think may work:

However, my suggestion is to use the correct event depending on the object you are using (i.e. RowDataBound, ItemDataBound, etc.)
This code will make it more clear and easier to maintain.
Also, you can get the code done quicker than trying to figure this out and wasting time on what should be easy, but is not.
 
Thanks for the reply. Still couldn't get that to work but have made a workaround for now.

In order to get the desired result I can display the string then add an asp:label that checks the length of the dataitem in the visible property:

Code:
<%#Eval("code").ToString() %>
<asp:Label runat="server" visible='<%#Eval("code").ToString().Length <=0 %>'>N/A</asp:Label>

still no idea why the original method wouldn't work despite that being a tried and tested method for many others.


Web Development Manager
 
I would just put the code in the correct event and do it in the code behind. It will make maintenance more clear and easy.
Just put a literal or label where you want the code and in the row or itemdatabound, set the text of the literal or label based on your needs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top