How can I access values in the Resource .resx file that is attached to custom control?

Posted on 2008-11-07
Last Modified: 2013-12-26
I have a directory called Controls. This folder contains all my custom controls.  To keep things nice and tidy, I also placed my resource files there. These resource files don't have to be application wide available through the app_GlobalResources folder, but only to the custom control.

There is a (simple) custom control called FooterControl.vb. When I add a resource file with the name FooterControl.resx, this is displayed as a child of the footercontrol.

In the Resource file, there is a value for btnSave, that contains the translation that should be displayed on the Text label. But when I want to assign the value I get stuck. Resources.FooterControl.btnSave doesn't work.
<DefaultProperty("Text"), ToolboxData("<{0}:FooterControl runat=server></{0}:FooterControl>")> _

    Public Class FooterControl

    Inherits WebControl

    Implements INamingContainer

    Implements IComponent

    Protected Overrides Sub CreateChildControls()

        Dim myButton As New Button

        myButton.Text = Resources.FooterControl.btnSave


    End Sub

End Class

Open in new window

Question by:TinoNL
    LVL 16

    Accepted Solution

    try this
    globalresourcestring = (String)GetGlobalResourceObject("MyResource", "GlobalResourceString1");

    in i guess it like

    globalresourcestring = DirectCast(GetGlobalResourceObject("MyResource", "GlobalResourceString1"), String)

    here MyResource is your resource file name

    Author Comment

    I'm afraid this doesn't work from a custom control. In a CC it should be something like:
    HttpContext.GetGlobalResourceObject("FooterControl", "btn_Save")
    It gives the error:
    Could not find any resources appropriate for the specified culture or the neutral culture.  Make sure "Resources.FooterControl.resources" was correctly embedded or linked into assembly "App_GlobalResources.iijgpt85" at compile time, or that all the satellite assemblies required are loadable and fully signed.
    So then I tried approaching the file through the local object:

    HttpContext.GetLocalResourceObject("FooterControl", "btn_Delete")
    It throws the error:
    The relative virtual path 'FooterControl' is not allowed here.
    LVL 16

    Assisted Solution

    by:Gyanendra Singh
    you are right... you are very near to solution :)  i guss problem in namespace difference between your control in aspx page

    so use this
    HttpContext.GetGlobalResourceObject("[Namespace.]ClassKey", "btn_Save")

    so provide fully qualified name


    Author Comment

    Nah; it looks like the globalresourceobject only works when your resx files reside in the app_GlobalResources folder?
    But it brought me the idea to explore the class view a little bit further and found the solution myself.
    Thanks for leading me this way.


    Open in new window

    LVL 16

    Expert Comment

    by:Gyanendra Singh
    ye ... that's why we called them gloabal resource ... . i am glad that you find a way .. all the best

    Author Closing Comment

    Thanks for the help man!

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Suggested Solutions

    Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now