views:

302

answers:

4

If dataitem is Null I want to show 0

<asp:Label ID="Label18" Text='<%# Eval("item") %>' runat="server"></asp:Label>

how can I accomplish this?

Thanks

A: 

Try replacing <%# Eval("item") %> with <%# If(Eval("item"), "0 value") %> (or <%# Eval("item") ?? "0 value" %>, when using C#).

Heinzi
I haven't tested your code, but "<%# If(Eval("item"), "0 value") %>" looks a bit odd. Not sure you can actually use an If statement with Eval like that, and wouldn't the result just be a True/False returned?
Jason Snelders
No, that's the VB.NET binary `If` operator: `If(value, valueIfNull)`: http://msdn.microsoft.com/en-us/library/bb513985.aspx
Heinzi
A: 

I don't know ASP.NET very well, but can you use the ternary operator?

http://en.wikipedia.org/wiki/Ternary%5Foperation

Something like: (x=Eval("item")) == Null ? 0 : x

Kristopher Ives
+1  A: 

You can also create a public method on the page then call that from the code-in-front.

e.g. if using C#:

public string ProcessMyDataItem(object myValue)
{
  if (myValue == null)
  {
     return "0 value";
  }

  return myValue.ToString();
}

Then the label in the code-in-front will be something like:

<asp:Label ID="Label18" Text='<%# ProcessMyDataItem(Eval("item")) %>' runat="server"></asp:Label>

Sorry, haven't tested this code so can't guarantee I got the syntax of "<%# ProcessMyDataItem(Eval("item")) %>" entirely correct.

Jason Snelders
A: 

Moreover, you can use (x = Eval("item") ?? 0) in this case.

http://msdn.microsoft.com/en-us/library/ms173224.aspx

Alexei Pshenichnyi
Thanks..........
Muhammad Akhtar