2009-11-03 53 views
2

我想填充從.resx文件中提取的數據的下拉列表。我希望能夠以.resx文件的名稱傳遞5個不同的函數,並以某種方式進行強制轉換,以便使用GetReourceSet進行檢索。Casting .resx類獲取ResourceSet

這裏是目前我在做什麼:

protected void populateCountryPickList(DropDownList whatDropDownList) 
{ 
    ResourceSet rs = Resources.CountryPickList.ResourceManager.GetResourceSet(CultureInfo.CurrentCulture, true, true); 
    whatDropDownList.DataSource = rs; 
    whatDropDownList.DataTextField = "Value"; 
    whatDropDownList.DataValueField = "Key"; 
    whatDropDownList.DataBind(); 
} 

在上面的例子中我有一個名爲CountryPickList.resx一個.resx文件,以便它只是使用的.resx名稱的問題,使用GetResourceSet的檢索的ResourceSet ...

我希望能夠做的是弄清楚如何我可以傳遞一個字符串到我的函數與.resx的名稱,並獲得相同的結果。

任何建議將不勝感激!

歡呼聲,

rise4peace

回答

2

ResourceManagerconstructor,需要一個resources文件的名稱。

你可以寫,因此下面的代碼:

ResourceSet rs = new ResourceManager(whatDropDownList.Name, GetType().Assembly) 
        .GetResourceSet(CultureInfo.CurrentCulture, true, true); 

編輯:在ASP.Net,這是不太一樣的。您可以查看其中一個資源文件的生成源代碼(.designer.cs),並查看靜態ResourceManager屬性的實現。

它看起來像這樣:

ResourceManager temp = new ResourceManager("Resources.Resource1", Assembly.Load("App_GlobalResources")); 
+0

這裏的功能: 保護無效populateDropDownList(DropDownList的whatDropDownList,串whatResourceName) { 的ResourceSet RS =新的ResourceManager(whatResourceName,的GetType()大會).GetResourceSet (CultureInfo.CurrentCulture,true,true); whatDropDownList.DataSource = rs; whatDropDownList.DataTextField =「Value」; whatDropDownList.DataValueField =「Key」; whatDropDownList.DataValueField =「Key」; whatDropDownList.DataBind(); } 如果我在App_GlobalResources中有一個名爲statelist.resx的.resx文件。我假設我可以傳遞狀態列表到這個函數? – rise4peace 2009-11-03 14:11:34

+1

完美!謝謝SLaks! – rise4peace 2009-11-03 18:19:48