How to use .resx and .resource files in custom server control asp?

3.1k views Asked by At

I am writing my own server side control and I am using images that are being stored in a .resx file. In the console application this code works fine:

     ResXResourceReader rsxr = new ResXResourceReader("Resource1.resx");
        
     foreach (DictionaryEntry d in rsxr)
     {
        Console.WriteLine(d.Key.ToString() + ":\t" + d.Value.ToString());
     }

     rsxr.Close();

but here

      protected override void RenderContents(HtmlTextWriter output)
    {
        ResXResourceReader rsxr = new ResXResourceReader("Resource1.resx");
  
        base.RenderContents(output);

        foreach (DictionaryEntry d in rsxr)
        {
            output.Write(d.Key.ToString());
        }
        
    }

I get this error:

Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\Resource1.resx'

I tried to use the ResourceManager, but it requires a .resource file. I can't access the resgen tool (command prompt does not understand the resgen command) and install it (during the attempt some errors ocured).

My questions are:

  1. Why can't I read .resx?

  2. How to install the resgen tool properly?

thanks.

2

There are 2 answers

1
Prashant Lakhlani On

It is considered good practice to store your resource file under App_GlobalResources folder in application root or in App_LocalResources with the same name as your user control file. So for example user user control is uc.ascx file in local resource folder should be uc.ascx.resx. That way it is easier to maintain and asp.net will automatically detect it.

Now your answers:

First Question: Use Server.MapPath("~/") points to physical directly where your web.config is. If you want to use a resource file in Controls folder you have to write Server.MapPath("~/Controls/Resource1.resx") to get the path.

Not sure what you want to do with resgen tool? When you compile your application, resource file will also be compiled. select your resource file and click F4, it will show you build action, choose resource in build action and your resource file will be included in assembly.

You can review this post for more information: How to use image resource in asp.net website?

0
Matt On

From your description, I understand you need to locate and access the user control's resource file. I found that it works nicely the following way:

  1. Create a App_GlobalResources on project level (via context menu Add -> Add ASP.NET Folder -> App_GlobalResources)

  2. Create the ressource file with the same name as the control, but inside the App_GlobalResources. For example, if the control is named myControl.ascx, then the ressource file's name for the default language has to be myControl.ascx.resx

  3. Create additional ressource files for each language you require. For instance, if you need German ("de-DE"), then add myControl.ascx.de.resx

  4. Add the class MultiLanguageUserControl as follows:

    public class MultiLanguageUserControl : System.Web.UI.UserControl
    {
        public string getResValue(string id)
        {
            var ctrlPath = TemplateControl.AppRelativeVirtualPath;
            var ctrlFile = System.IO.Path.GetFileName(ctrlPath);
            var resObj = GetGlobalResourceObject(ctrlFile, id);
            if (resObj!=null)
                        return resObj.ToString();
            else
                        return string.Format("UNRESOLVED[{0}]", id);        
        }
    }
    
  5. Open the code behind of myControl and make it inherit from MultiLanguageUserControl instead from System.Web.UI.UserControl:

    public partial class myControl : MultiLanguageUserControl { //... }

  6. In the HTML code, use the new function, e.g.: <%=getResValue("resid")%>, where "resid" is the name of the ressource string you want to look up. You can also use the HTML-encoding tag <%: instead of <%=, depending on your requirements. Alternatively, you can use getResValue anywhere in your server-sided C# code in your user control to retrieve the value from the ressource file.

  7. Ensure that you support the language detection in the Page_Load event of the page, which uses the user control. How you can do this is described here (look for the function InitializeCulture).

NOTE: If you want to read the page's local resource strings from inside the user control then take a look here.