2010-07-13 6 views
2

안녕하세요. 제발 도와주세요. GridView itemtemplate 내에서 클라이언트 측에 조건을 작성해야합니다.Gridview itemtemplate 내에서 클라이언트 측에서 조건을 작성하는 방법은 무엇입니까?

아래 볼 수 있지만이 작동하지 않습니다처럼 ...

<% if(Eval("item").Contains("keyword"){%> 

<img src='<# Eval("imagepath") %>' /> 

<%} 
else if(Eval("item").Contains("keyword2")){ 
%> 
<img src='<# Eval("imagepath2") %>' /> 

<%}%> 

답변

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; 
} 
+0

를 반환, 단지 보호 부울로 변경 // 서버 ImgPath (객체 항목) { bool result = false; if (items.ToString(). Contains ("keyword")) { result = true; } 반환 결과; } – German

0

이 더 나은 방법 제 생각에 제안했다.

// 클라이언트 측

<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; 
    } 
0
<img src='<# (Eval("imagepath") + string.Empty).Contains("keyword") ? Eval("imagepath") + string.Empty : Emal("Imagepath2") + string.Empty %>' /> 

평가는 ("") 나를 위해 작동하는 object

관련 문제