【问题标题】:listview null values列表视图空值
【发布时间】:2010-08-03 08:58:02
【问题描述】:

如果字段为空,我不希望标签显示。在我的数据库行中,所有列的数据都不完整。

我认为这会起作用:

 <% if(# Eval("recipe_by") == null){%><br /><br /><%} else {%>Recipe by:
<br /><br /> <asp:Label ID="recipe_byLabel" Font-Bold="True" runat="server" Text='<%# Eval("recipe_by") %>' /> }

我收到此错误:

编译器错误消息:CS1040: 预处理器指令必须显示为 第一个非空白字符 一行

来源错误:

第 386 行:第 387 行:行 388:

配方 作者:第 389 行:

' />} 第 390 行:

编译器错误消息:CS1040: 预处理器指令必须显示为 第一个非空白字符 一行

来源错误:

第 386 行:第 387 行:行 388:

配方 作者:第 389 行:

' /> } 第 390 行:

【问题讨论】:

    标签: c# asp.net listview null label


    【解决方案1】:

    它是井号字符 (#),我们在 blighty 中称它为 - 我很确定你不想要那个。用于Preprocessor Directives。

    【讨论】:

    • 我从别人那里发布的答案实际上没有用,当为空时仍然显示标签。我取出第一个 # charater 得到这个错误 Compiler Error Message: CS1010: Newline in constant Source Error: Line 407: Serving size:

      Line 408:
    【解决方案2】:

    在这里

    <span>  
     <%# (Eval("recipe_by")==null)? "<br/><br/>":"Recipe By:<br/><br/>" %></span>  
        <asp:Label ID="recipe_byLabel" Font-Bold="True" runat="server" Text='<%# Eval("recipe_by") %>' />  
    

    【讨论】:

      【解决方案3】:
      Compiler Error Message: CS0029: Cannot implicitly convert type 'string' to 'bool'
      
      Source Error:
      
      
      
      Line 388:         
      Line 389:
      Line 390:<asp:Label ID="recipe_byLabel" Font-Bold="True" Text="Recipe by:" runat="server"  
      Line 391: Visible='<%# (Convert.ToString(Eval("recipe_by")))? Boolean.Parse("false"):Boolean.Parse("true") %>'></asp:Label>  
      Line 392: 
      

      我本可以发誓这样做的!

      【讨论】:

        【解决方案4】:

        如果您厌倦了尝试调整内联标签以使其正常工作,您可以使用其他两种方法:

        • 代码隐藏
        • 存储过程/sql语句返回

        Code behind 是我最喜欢的——如果您正在对 gridview 等对象进行操作,您可能必须使用 onrowdatabound 或其他类似事件来设置可见属性。

        如果您必须使用内联标签,有时,在 SQL 语句中添加一个“可见”列可能会更容易——即:

        SELECT
           field1,
           field2,
           CASE WHEN recipe_by is null, 'false' ELSE 'true' END as recipe_by_visible
        FROM
           table
        

        然后在你的内联代码中:

        ...
        visible = <% eval("recipe_by_visible") %>
        ...
        

        【讨论】:

          【解决方案5】:

          这就是我最终使用的,,,效果很好!!

          <span><%# (String.IsNullOrEmpty(Convert.ToString(Eval("yield"))))? "":" <br/><br/>Yield:<br/><br/>" %></span>  
          <asp:Label ID="yieldLabel" Font-Bold="True" runat="server" Text='<%# Eval("yield") %>' />  
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多