Using '<%# Eval("item") %>'; Handling Null Value and showing 0 against

Muhammad Akhtar picture Muhammad Akhtar · Dec 30, 2009 · Viewed 186.6k times · Source

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?

Answer

Jason Snelders picture Jason Snelders · Dec 30, 2009

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.