2009-08-06 65 views
0

我正在做一些標籤。我想一個資源鍵(S,秒)添加到我的組合框一個與項目[3秒,5秒,10秒,30秒......],並用它喜歡:如何以這種方式使用ASP.NET資源?

Text="3<%$ Resources: myResource, s%>" to get comboBoxItem 3sec, 
Text="5<%$ Resources: myResource, s%>" to get comboBoxItem 5sec ... 

但我發現,服務器會將其視爲純文本。

我是否需要在單獨的資源密鑰對中定義每個項目?

回答

0

你可以做這樣的(用的DataBind()調用的地方):

Text='<%# "3" + Resources.myResource.s %>' 

編輯:您也可以從代碼做到這一點後面。事情是這樣的:

int[] times = new int[]{ 3, 5, 10, 30 }; 
foreach (int time in times) 
{ 
    string text = time.ToString() + Resources.myResource.s; 
    cbo.Items.Add(new ListItem(text, time.ToString())); 
} 

編輯2:按照穆罕默德的觀察的第一個例子不適合這項工作。我用它來進行其他控制,而且我沒有看到目前的情況,這是不正確的。鑑於這一點,我會填補後面的代碼控制。

+1

我已經檢查了,這是給error..Text ='<%# 「3」 +資源.myResource.s%>' – 2009-08-06 06:39:40

0

您可以嘗試創建自己的custom expression builder。如果你想實現你在問題中提出的問題,他們真的很強大。

或者,您可以隨時使用服務器端代碼。

2

考慮定義如下資源文件項值:

{0}sec 

然後

String.Format(Resources.myResource.s, "3") 
+0

你打算如何在aspx/ascx頁面中寫入? – Kirtan 2009-08-06 07:07:08

+0

也許就像:<%#String.Format(「3 {0}」,Resources.myResource.s)%>'。我不知道本的方法是否可行,但如果這樣做,將是一個非常酷的方式。 +1 – Cerebrus 2009-08-06 07:31:13

+0

本的方法似乎不工作...... – Jay 2009-08-06 08:30:18