Search code examples
asp.netresx

Resources cannot be accessed in aspx page


i have a solution in which i have a web project and couple of other projects. I have added another project that has nothing but a resx file. I have referenced this resource project dll into the web project. is there any possible way i could access the resource in dll into the aspx page. For ex: <asp:Button ID="Button1" runat="server" Text="<%$ Resources:Resource,ButtonName %>"> The ButtonName must be accessed from the resourcedll.


Solution

  • Import the namespace into the aspx page using @Import page directive.

    <%@ Import Namespace = "MyProject.Resources" %>
    

    Now to use the resource for setting the property of a Server control, you need to call DataBind() method at the page level on your Page_Load() event. (can be invoked for specific controls also).

        Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();
            }
        }
        in your aspx page:
        <asp:Button ID="Button1" runat="server" Text = '<%# ProjectResources.CmdBtn %>' />
    

    make sure you make the resource class and the resource key property public, its internal by default.