views:

56

answers:

5

I don't want the label to show if the field is null. In my database rows, data isn't complete for all columns.

I thought this would work:

 <% if(# Eval("recipe_by") == null){%><br /><br /><%} else {%>Recipe by:
<br /><br /> <asp:Label ID="recipe_byLabel" Font-Bold="True" runat="server" Text='<%# Eval("recipe_by") %>' /> }

I get this error:

Compiler Error Message: CS1040: Preprocessor directives must appear as the first non-whitespace character on a line

Source Error:

Line 386: Line 387: Line 388:<% if(# Eval("recipe_by") == null){%>

<%} else {%>Recipe by: Line 389:

' /> } Line 390:

Compiler Error Message: CS1040: Preprocessor directives must appear as the first non-whitespace character on a line

Source Error:

Line 386: Line 387: Line 388:<% if(# Eval("recipe_by") == null){%>

<%} else {%>Recipe by: Line 389:

' /> } Line 390:

+1  A: 

It's the hash character (#), as we call it in blighty - I'm pretty sure you don't want that there. That's used for Preprocessor Directives.

Grant Crofton
The answer I posted from someone else actually didn't work, still shows the label when null. I took out the first # charater and got this errorCompiler Error Message: CS1010: Newline in constantSource Error: Line 407: Serving size:<br /><br />Line 408: <asp:Label ID="serving_sizeLabel" Font-Bold="True" runat="server" Line 409: Text='<%# Eval("serving_size") %>' />Line 410: Line 411:
Mike
A: 

Here it is

<span>  
 <%# (Eval("recipe_by")==null)? "<br/><br/>":"Recipe By:<br/><br/>" %></span>  
    <asp:Label ID="recipe_byLabel" Font-Bold="True" runat="server" Text='<%# Eval("recipe_by") %>' />  
Mike
nope didn't work, just looked like it.
Mike
A: 
Compiler Error Message: CS0029: Cannot implicitly convert type 'string' to 'bool'

Source Error:



Line 388:         
Line 389:
Line 390:<asp:Label ID="recipe_byLabel" Font-Bold="True" Text="Recipe by:" runat="server"  
Line 391: Visible='<%# (Convert.ToString(Eval("recipe_by")))? Boolean.Parse("false"):Boolean.Parse("true") %>'></asp:Label>  
Line 392: 

I could've swore that would do it!

Mike
A: 

If you get tired of trying to tweak the inline tags to work right, you could use two other approaches:

  • code behind
  • stored procedure / sql statement return

Code behind is my favorite -- if you're operating on an object like a gridview, etc., you might have to use the onrowdatabound or other similar event to set visible properties.

If you must use inline tags, sometimes, maybe it would be easier just to have a "visible" column in your SQL Statement -- i.e.:

SELECT
   field1,
   field2,
   CASE WHEN recipe_by is null, 'false' ELSE 'true' END as recipe_by_visible
FROM
   table

And then in your inline code:

...
visible = <% eval("recipe_by_visible") %>
...
dave
A: 

This is what I ended up using,,, works GREAT!!

<span><%# (String.IsNullOrEmpty(Convert.ToString(Eval("yield"))))? "":" <br/><br/>Yield:<br/><br/>" %></span>  
<asp:Label ID="yieldLabel" Font-Bold="True" runat="server" Text='<%# Eval("yield") %>' />  
Mike