Eval() display custom value if null

坚强是说给别人听的谎言 提交于 2019-12-02 06:47:27

问题


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

Does anyone know how this works? I want to display "NA" if there is no value provided to TypeOfPainting.


回答1:


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.




回答2:


Well you can try to do something like:

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



回答3:


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

If (string.IsNullOrEmpty(TypeofPaintingValue))
{
  TypeofPainting.Text="NA";
}



回答4:


you can set this things from database side also

ISNULL(TypeOfPainting,'NA') AS TypeOfPainting



回答5:


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


来源:https://stackoverflow.com/questions/16690496/eval-display-custom-value-if-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!