<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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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.
Method 2
Your control is runat="server" Why dont you control the value in codebehind?
If (string.IsNullOrEmpty(TypeofPaintingValue))
{
TypeofPainting.Text="NA";
}
Method 3
Well you can try to do something like:
<%#(string.IsNullOrEmpty(Eval("TypeOfPainting").ToString()) ? "NA" : Eval("TypeOfPainting"))%>
Method 4
you can set this things from database side also
ISNULL(TypeOfPainting,'NA') AS TypeOfPainting
Method 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
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0