How to write condition on client side within gridview itemtemplate?
Hi could you help me please. I need to write condition on client side within GridView itemtemplate.
Like you can see below but it is not working ...
<% if(Eval("item").开发者_运维知识库Contains("keyword"){%>
<img src='<# Eval("imagepath") %>' />
<%}
else if(Eval("item").Contains("keyword2")){
%>
<img src='<# Eval("imagepath2") %>' />
<%}%>
Use a code-behind function to return a boolean.
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")) %>' />
Code-Behind:
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;
}
I modified code Rick Schott suggested in my opinion this way better.
//Client side
<img src='<%# ImgPath(Eval("items")) %>' id="Img" runat="server" />
//Server side
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;
}
<img src='<# (Eval("imagepath") + string.Empty).Contains("keyword") ? Eval("imagepath") + string.Empty : Emal("Imagepath2") + string.Empty %>' />
Eval("") return an object
精彩评论