How to check if Eval has null value?

3

I'm using the Eval property to load a field from the GridView instantiated of an object, the problem is when the object has no value, instead of being blank I wanted to show a trace indicating that nothing was found.

The code I'm using is this

<%# if(Eval("IdEntradaItem")== "" ||Eval("IdEntradaItem")==null)?" - ": Eval("IdEntradaItem") %> 
    
asked by anonymous 29.06.2015 / 17:07

2 answers

0

You can create a public method on the bean to do the validation:

public string ValidarItem(object valor)
{
  if (valor == null)
  {
     return "xpto";
  }

  return valor.ToString();
}

And in .aspx:

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

EDIT:

You can also do this:

<asp:Label ID="label1" Text='<%# Eval("item") ?? "valor_exibir_se_eval_null" %>' runat="server"></asp:Label>
    
29.06.2015 / 17:23
0

Use this way and see if it works:

<%# if(Eval("IdEntradaItem").ToString() == string.Empty) ? " - " : Eval("IdEntradaItem").ToString() %>
    
29.06.2015 / 19:49