2011-03-15 49 views
0

有一個綁定到ListView的對象列表,用於列出這些項目的好列表。在這個ListView中,我有1列應該具有顯示特定字符​​串的特定條件。這可能使用內聯代碼,或者我應該使用代碼隱藏得到解決方法嗎?ASP.Net C#ListView內聯條件

這是我想要做什麼:

<% if (((Recipe)Container.DataItem).Status == RecipesModel.REJECTED) { %> 
Something goes here 
<% } %> 

但這返回此異常:

The name 'Container' does not exist in the current context 

編輯:此代碼用於內部<ItemTemplate>

EDIT 2:我發現自己使用下面的代碼來解決這個問題:

<asp:PlaceHolder id="place_public" runat="server" Visible='<%# ((Recipe)Container.DataItem).Status == RecipesModel.VALIDATED %>'> 
Something here 
</asp:PlaceHolder> 
+0

看起來像容器不存在於這種情況下... – TJHeuvel 2011-03-15 15:14:40

回答

0

不能使用數據的Container.DataItem外結合上下文

嘗試像

<%# Container.DataItem ... %> 

如:

<%# ((String)Container.DataItem).ToUpper() == "test" ? "IsTest" : "NotTest" %> 
+0

但我不能用它來測試一個條件 – Jens 2011-03-15 15:25:29

+0

@Jens看我的例子。 – 2011-03-15 15:28:12

+0

是否有可能像我的代碼一樣從RecipesModel類中對常量進行測試? – Jens 2011-03-15 15:30:13

0

您可能想要在.aspx頁面中導入容器類的名稱空間。 例如:

<%@ Import Namespace="Container Class namespace" %> 
+0

我沒有寫我自己的Container類,這是ListView的容器。我不知道我應該包括什麼名字空間。 – Jens 2011-03-15 15:22:54

0

它看起來像你想在其範圍之外使用Container對象。你可以發佈其餘的代碼,以便我們可以看到頁面上發生了什麼?

+0

此代碼在ItemTemplate標籤內部使用。像<%#Eval(「status」)%>這樣的表達式可以工作,但是我想在這些值中測試一個特定的條件。 – Jens 2011-03-15 15:28:20