就像你可以在下面看到的那样但它不起作用......
<% if(Eval("item").Contains("keyword"){%>
<img src='<# Eval("imagepath") %>' />
<%}
else if(Eval("item").Contains("keyword2")){
%>
<img src='<# Eval("imagepath2") %>' />
<%}%>
答案 0 :(得分:2)
使用代码隐藏函数返回布尔值。
ASPX:
<img src='/path1.jpg' id="img1" runat="server"
visible='<%# ShowImg1(Eval("Item")) %>' />
<img src='/path2.jpg' id="img2" runat="server"
visible='<%# ShowImg2(Eval("Item")) %>' />
<强>代码隐藏:强>
protected boolean ShowImg1(object item)
{
bool result = false;
string item = object as string;
// do your checks and return true or false;
return result;
}
protected boolean ShowImg2(object item)
{
bool result = false;
string item = object as string;
// do your checks and return true or false;
return result;
}
答案 1 :(得分:0)
我修改了Rick Schott的代码,我认为这样做更好。
//客户端
<img src='<%# ImgPath(Eval("items")) %>' id="Img" runat="server" />
//服务器端
protected string ImgPath(object items)
{
var result = "";
if (items.ToString().ToLower().Contains("keyword"))
{
result = "path_to_image";
}
else if (items.ToString().ToLower().Contains("keyword2"))
{
result = "path_to_image_2";
}
else
{
result = "path_to_image";
}
return result;
}
答案 2 :(得分:0)
<img src='<# (Eval("imagepath") + string.Empty).Contains("keyword") ? Eval("imagepath") + string.Empty : Emal("Imagepath2") + string.Empty %>' />
Eval(“”)返回object