问题
<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