2012-12-28 45 views
0

我想在Asp.Net ListView的源視圖中輸入邏輯。問題在於執行「If(isItTrue(test))」時,程序在屏幕上寫入false或true。有誰知道如何解決這個問題?設計視圖ListView ASP.NET邏輯

<%# test= Eval("testId")%> 
      <% 
       If (isItTrue(test)) Then 

       %> 
      <asp:Button ID="btnTest" runat="server" Text="Like" /> 
      <% 
      Else 
       %> 
       <asp:Label runat="server" Text="hello" </asp:Label> 

      <% 
      End If 
       %> 
+0

你可以展示你在哪裏使用ListView的方法嗎? – MethodMan

回答

1

您可以使用ItemDataBound來檢查這樣的信息並使用您的條件顯示或隱藏控件。嘗試這樣的事情在你的代碼behine:

protected void ListViewTest_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    // if it is data item 
    if (e.Item.ItemType == ListViewItemType.DataItem) 
    { 
     // call your function 
     if (isItTrue("test")) 
     { 
      // show the button 
      e.Item.FindControl("btnTest").Visible = true; 
     } 
     else 
     { 
      // show the label 
      e.Item.FindControl("lblTest").Visible = true; 
     } 
    } 
} 

而在你的列表視圖,你可以做這樣的事情,在佔位設置事件並添加控件

<asp:ListView ID="ListViewTest" DataSourceID="..." OnItemDataBound="ListViewTest_ItemDataBound" runat="server"> 
    <LayoutTemplate> 
     <table> 
     <tr> 
      <th>Column Name</th> 
     </tr> 
     <tr runat="server" id="itemPlaceholder" /> 
     </table>   
    </LayoutTemplate> 
    <ItemTemplate> 
     <tr style="background-color: #CAEEFF" runat="server"> 
     <td> 
      <%-- both controls are here --%>  
      <asp:Button ID="btnTest" runat="server" Visible="false" Text="Like"></asp:Button> 
      <asp:Label ID="lblTest" runat="server" Visible="false" Text="hello"></asp:Label> 
     </td> 
     </tr> 
    </ItemTemplate> 
    </asp:ListView> 
0

你確定它的不是這一行:<%# test= Eval("testId")%>這是寫入真或假的輸出?