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

前端 未结 11 1674
清酒与你
清酒与你 2020-12-04 12:14

If dataitem is Null I want to show 0

\' runat=\"server\">

        
相关标签:
11条回答
  • 2020-12-04 12:40

    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.

    0 讨论(0)
  • 2020-12-04 12:45

    I'm using this for string values:

    <%#(String.IsNullOrEmpty(Eval("Data").ToString()) ? "0" : Eval("Data"))%>
    

    You can also use following for nullable values:

    <%#(Eval("Data") == null ? "0" : Eval("Data"))%>
    

    Also if you're using .net 4.5 and above I suggest you use strongly typed data binding:

    <asp:Repeater runat="server" DataSourceID="odsUsers" ItemType="Entity.User">
        <ItemTemplate>
            <%# Item.Title %>
        </ItemTemplate>
    </asp:Repeater>
    
    0 讨论(0)
  • 2020-12-04 12:46

    I have tried this code and it works well for both null and empty situations :

    '<%# (Eval("item")=="" || Eval("item")==null) ? "0" : Eval("item")%>'
    
    0 讨论(0)
  • 2020-12-04 12:47

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

    0 讨论(0)
  • 2020-12-04 12:50

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

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

    0 讨论(0)
提交回复
热议问题