How to check if Eval has null value?

Asked

Viewed 415 times

6

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

the code I’m using is this

<%# if(Eval("IdEntradaItem")== "" ||Eval("IdEntradaItem")==null)?" - ": Eval("IdEntradaItem") %> 
  • You forgot to post the code, post please.

2 answers

1

You can create a public method in the bean to validate:

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

  return valor.ToString();
}

And no . aspx:

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

EDIT:

You can also do so:

<asp:Label ID="label1" Text='<%# Eval("item") ?? "valor_exibir_se_eval_null" %>' runat="server"></asp:Label>
  • the code I am using is this <%# if(Eval("Identradaitem")== "" ||Eval("Identradaitem")=null)?" - ": Eval("Identradaitem") %>

  • Thank you to everyone who helped me there solve me problem, but the solution that Caique. C proposed helped to solve the problem in question, is thank you even the people who dedicated to help me.

  • Still, I think you might have problems if your Eval returns null... I recommend using String.Isnullorempty(Eval("Identradaitem").Tostring()) as a "more complete solution".

  • @krispim, if an answer helped you solve the problem, signal the same so that other people who had the same difficulty can understand that there was solution.

  • @Marllonnasser, there is no need for verification, because the Eval method returns a 'string. Empty' if the object is null.

1

Use this way and see if it works:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.