i have a repeater. and it fill certain numeric values within anchor tag. but if the value is zero i just want to display a span and anchor is discarded.
this is my code.
in HTML
<td>
<%if (ProcessMyDataItem(Eval("7DAYS").ToString()))
{%>
<a class='aDetails' href="DashCheckInCheckOutDetails.aspx?building=<%=hiddenBuildingDetailsId.Value.ToString() %>&action=<%#Eval("ID") %>&day=7&name=<%#Eval("DESC") %>"><%#Eval("7DAYS") %> </a>
<% }
else
{
Response.Write("<span>0</span>");
}
%>
</td>
and in the codebehind i have
public bool ProcessMyDataItem(string myValue)
{
if (myValue == "0")
{
return false;
}
return true;
}
but when i run it, shows an error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
is there any way to check eval value is zero and if zero, display span, else display anchor tag.
Try using:
<%#Eval("7DAYS").ToString() != "0" ?"<a class='aDetails' href='DashCheckInCheckOutDetails.aspx?...'>3 </a>":" <span>0</span>"%>
This will work! i have tested!
NOTE: please add the url
for the link as per your need.
Updated Answer:
<%#Eval("7DAYS").ToString() != "0" ? "<a class='aDetails' href=DashCheckInCheckOutDetails.aspx?building="+hiddenBuildingDetailsId.Value+"&action="+Eval("ID") +"&day=7&name="+Eval("DESC") +">"+Eval("7DAYS")+" </a>" : " <span>0</span>" %>
This will work.
This is what I think
<%# Eval("7DAYS") == 0 ? "empty" : "notempty" %>
Also if the field value type is supposed to be string you could do something along the lines of..
<%# (Eval("7DAYS") as string) ?? "empty" %>
You must change your 'a tag' like this,
<a class="aDetails" href="DashCheckInCheckOutDetails.aspx?building="+<%hiddenBuildingDetailsId.Value.ToString() %>&action=<%Eval("ID") %>&day=7&name=<%Eval("DESC") %>'><% Eval("7DAYS") %> </a>
Let me know the result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.