Using '<%# Eval(“item”) %>'

2019-01-13 05:16发布

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?

11条回答
贼婆χ
2楼-- · 2019-01-13 05:52

I use the following for VB.Net:

<%# If(Eval("item").ToString() Is DBNull.Value, "0 value", Eval("item")) %>
查看更多
走好不送
3楼-- · 2019-01-13 05:52

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

查看更多
smile是对你的礼貌
4楼-- · 2019-01-13 05:59

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

'<%# (Eval("item")=="" || Eval("item")==null) ? "0" : Eval("item")%>'
查看更多
够拽才男人
5楼-- · 2019-01-13 05:59

Used a modified version of Jason's answer:

public string ProcessMyDataItem(object myValue)
{
  if (myValue.ToString().Length < 1)
  {
     return "0 value";
  }

  return myValue.ToString();
}
查看更多
唯我独甜
6楼-- · 2019-01-13 06:01

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>
查看更多
时光不老,我们不散
7楼-- · 2019-01-13 06:08

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

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

查看更多
登录 后发表回答