views:

194

answers:

1

Hello,

I am trying to populate a dropdownlist with data pulled from a .resx file. Instead of having 5 different functions I'd like to be able to pass in the name of the .resx file and cast it somehow so I can retrieve it using GetReourceSet.

Here's what I'm currently doing:

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();
}

In the example above I have a .resx file named CountryPickList.resx so its just a matter of using the .resx name to retrieve the ResourceSet using GetResourceSet...

What I would like to be able to do is figure out how I can pass in a string to my function with the name of the .resx and obtain the same results.

Any suggestions would be greatly appreciated!

cheers,

rise4peace

A: 

ResourceManager has a constructor that takes the name of a resources file.

You can therefore write the following code:

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


EDIT: In ASP.Net, it's not quite the same. You can view the generated source code (.designer.cs) for one of your resource files and look at the implementation of the static ResourceManager property.

It looks like this:

ResourceManager temp = new ResourceManager("Resources.Resource1", Assembly.Load("App_GlobalResources"));
SLaks
Here's the function: protected void populateDropDownList(DropDownList whatDropDownList, string whatResourceName) { ResourceSet rs = new ResourceManager(whatResourceName, GetType().Assembly).GetResourceSet(CultureInfo.CurrentCulture, true, true); whatDropDownList.DataSource = rs; whatDropDownList.DataTextField = "Value"; whatDropDownList.DataValueField = "Key"; whatDropDownList.DataBind(); }If I have a .resx file named statelist.resx in the App_GlobalResources. I assume I can pass just statelist to this function?
rise4peace
perfect! Thank You SLaks!
rise4peace