using .resx && .resource files in custom server control asp

我怕爱的太早我们不能终老 提交于 2019-12-04 08:47:00
Prashant Lakhlani

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: 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?

Matt

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 nicely described here (look for the function InitializeCulture).

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!