Eval() display custom value if null

前端 未结 5 1397
猫巷女王i
猫巷女王i 2021-01-25 02:51

    \' />

<
相关标签:
5条回答
  • 2021-01-25 03:12

    I will suggest to do it in SQL only:

    using ISNULL(expression, value_if_expression_is_null) or

    COALESCE(expression, expression2, expression3)
    

    example :

    SELECT
      Name, DOB,
      (CASE WHEN Address1 IS NULL THEN 'NA' ELSE Address1 END) AS Address1,
      (CASE WHEN Address2 IS NULL THEN 'NA' ELSE Address2 END) AS Address2,
      ...
    FROM Users
    

    or

    SELECT 
      Name, DOB, Address1, 
      coalesce(Address2,'NA'), coalesce(City,'NA'), 
      coalesce(State,'NA'), coalesce(Zip,'NA')
    FROM Users
    
    0 讨论(0)
  • 2021-01-25 03:23

    Well you can try to do something like:

    <%#(string.IsNullOrEmpty(Eval("TypeOfPainting").ToString()) ? "NA" : Eval("TypeOfPainting"))%>
    
    0 讨论(0)
  • 2021-01-25 03:28

    Your control is runat="server" Why dont you control the value in codebehind?

    If (string.IsNullOrEmpty(TypeofPaintingValue))
    {
      TypeofPainting.Text="NA";
    }
    
    0 讨论(0)
  • 2021-01-25 03:31

    by creating a public method You can complete this task very easily like

    public string testbind(object myvalue)
    {
      if (myvalue == null)
      {
         return "NA value";
      }
    
      return myValue.ToString();
    }
    

    Label Code:

    <asp:Label ID="TypeOfPaintingLabel" Text='<%# testbind(Eval("TypeOfPainting")) %>' runat="server"></asp:Label>
    

    Or you may use

    <%#(String.IsNullOrEmpty(Eval("TypeOfPainting").ToString()) ? "NA" : Eval("TypeOfPainting"))%>
    

    You have to follow this type of scenarion.

    Hope it works.

    0 讨论(0)
  • 2021-01-25 03:33

    you can set this things from database side also

    ISNULL(TypeOfPainting,'NA') AS TypeOfPainting
    
    0 讨论(0)
提交回复
热议问题