Use resources in Class Library from WebForms app - c#

In my application there is one class library project and one website. I created a resource file in a class library and modifier of it is public. I need to use this resource in website layer:
<asp:Label ID="Label2" runat="server" Text="<%$ Resources:resourceName, ErrorEmptyName %>"></asp:Label>
But an error is shown:
The resource object with key 'ErrorEmptyName' was not found.
But in code-behind I access this key:
Label1.Text = resourceName.ErrorEmptyName;
and no error.
I find this page but i can not use:
Referencing resource files from multiple projects in a solution
What is wrong? How to do it?

You can use the code
<asp:Label ID="Label2" runat="server" Text="<%# resourceName.ErrorEmptyName %>"></asp:Label>
maybe you need to import your namespace.

Related

Accessing Properties.Resources from SharePoint ASPX page

This might be a pretty silly question, but I'm new to ASP.NET and I'm not able to pull values from myProject.Properties.Resources within the ASPX-page itself.
I've tried the below but the page fails to load
<asp:Label ID="Label1" runat="server" Text="<%$ Resources: Resources, string1 %>">
If it makes any difference, this page is a part of a SharePoint project and the page is in myProject/ADMIN/myProject/mypage.aspx
Any help here would be really appreciated

access resource text from markup page

I'm creating an web application with multiple languages.
I've set the culture like this
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
I've got several language files like "en.resx" and "de.resx".
I can read them from my code behind them like this
var test = GetGlobalResourceObject(Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk");
But how about from the markup page.
I've been searching the web and most pages is suggesting something like this
<asp:Literal Text='<%$ Resources:Resource, aboutUsLnk %>' runat="server" />
That works if I have a .resx file called Resource but i that's not what I want. What did I miss?
That works if I have a .resx-file called Resource but i thats not what I want. What did I miss?
You are probably looking for local resources and meta:resourcekey attribute.
Local are defined per page (you define exactly the same name as your page for them), you use them for storing resources specific to one page. You create them by adding ASP.NET specific folder (App_LocalResources) and then inside it local resources for each of your pages:
App_LocalResources/{pagename}.resx
And then call for resource objects from resource files (AboutUs.resx, AboutUs.fr-BE.resx,...) from the page markup (AboutUs.aspx) would be something like this:
<asp:Literal Text='About Us' meta:resourcekey="aboutUsLnk" runat="server" />
Global resources which you mentioned are defined accros the whole website (usually you store here resources like "Edit", "Save", etc.) and are usually called as you showed.
Read here for more details: http://msdn.microsoft.com/en-us/magazine/cc163566.aspx
EDIT
Ahh sorry for misunderstanding, you are probably asking how to call your global resources which names differentiate per culture. You can do that in your markup code almost the same as you are doing in code behind, using GetGlobalResourceObject.
Anywhere outside of server controls you can write:
<%= GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk")%>
To call GetGlobalResourceObject inside of server control attributes you cannot use <%= %>, but you can wrap the server controls around it (in the ones that allow this, like Label for example):
<asp:Label ID="Label1" runat="server"><%= GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk")%></asp:Label>
Or, you can use the binding syntax:
<asp:Label ID="Label1" runat="server" Text='<%# GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk")%>'></asp:Label>
Note, that when using the latter you will need to bind your control:
protected void Page_Load(object sender, EventArgs e)
{
Label1.DataBind();
}
EDIT 2
You can wrap the upper code in some helper method to improve code readabilty. In code behind you declare it:
protected string GetResource(string resourceName)
{
return GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), resourceName).ToString();
}
And in markup you can call it similiar as previous:
<asp:Label ID="Label1" runat="server" Text='<%# GetResource("aboutUsLnk")%>'></asp:Label>
<asp:Label ID="Label2" runat="server"><%= GetResource("aboutUsLnk")%></asp:Label>

what am i missing to use embedded resources?

I am using Asp.net with c# in Visual studios 2012.
I added the images i need into the solution by
Creating a folder
rightclicking and selecting add-existing
selected image file
right clicked image - properties - build action embedded resource
I went into the project properties assembly info.cs and added references to my resources.
[assembly: WebResource("FIMS_Courses.Images.FIMSNext.gif", "image/gif")]
[assembly: WebResource("FIMS_Courses.Images.FIMSPrevious.gif", "image/gif")]
[assembly: WebResource("FIMS_Courses.Images.FIMSCourselist.gif", "image/gif")]
[assembly: WebResource("FIMS_Courses.Images.FIMStopofpage.gif", "image/gif")]
now i am trying to access them.
Inside my asp potion of my program i select them by typing imageUrl= and then intellesense lets me choose the path; This is my code:
<asp:ImageButton ID="PreviousListLink" ImageUrl="~/Images/FIMSCourselist.gif" CssClass="floatleft" runat="server"/> <asp:ImageButton ID="PreviousButtonTop" ImageUrl="~/Images/FIMSPrevious.gif" runat="server"/> <asp:ImageButton ID="NextButtonTop" ImageUrl="~/Images/FIMSNext.gif" runat="server" />
<h4><asp:Label ID="CourseNumber" Text="####" runat="server"/> <asp:Label ID="CourseTitle" Text="This is a course Title" runat="server" /></h4>
<asp:Label id="Desc" text="Course Description" />
<asp:Image ID="TopOfPageButton" ImageUrl="~/Images/FIMStopofpage.gif" runat="server" /> <asp:ImageButton ID="PreviousButtonBottom" ImageUrl="" runat="server" /> <asp:ImageButton ID="NextButtonBottom" ImageUrl="" runat="server"/>
and when i give it a test run i get nothing showing up. no images what so ever.
am i missing some crucial steps to access embedded resources?
You need to reference your embedded resources using the code-behind.
Example:
string topOfPageImageUrl = Page.ClientScript.GetWebResourceUrl(this.GetType(),
"FIMS_Courses.Images.FIMStopofpage.gif");
TopOfPageButton.ImageUrl = topOfPageImageUrl;

Why is AntiXss Library not working with Eval()?

I am working on a simple ASP.NET application to prove the use of AntiXss library. The library is very powerful and it is working fine with me except with Eval() in aspx pages. For example, if I have a label control like the following:
<asp:Label runat="server" ID="CommentsLabel" Text='<%# Eval("Name") %>' />
How will I be able to use AntiXss with it?
I followed this example mentioned HERE, and I added the library to the label control by doing the following:
<asp:Label runat="server" ID="CommentsLabel" Text='<%#Microsoft.Security.Application.AntiXss.HtmlEncode(((System.Data.DataRowView)Container.DataItem)["Comments"].ToString()) %>'/>
And my instructor is still telling me it is vulnerable and I don't know why. Then, instead of using AntiXss.HtmlEconde() , I used Encoder.HtmlEncode() in the previous line
<asp:Label runat="server" ID="CommentsLabel" Text='<%#Microsoft.Security.Application.Encoder.HtmlEncode(((System.Data.DataRowView)Container.DataItem)["Comments"].ToString()) %>'/>
EDIT:
And based on the scanning tool which my instructor is using it, he is still telling me that it is vulnerable. So how to use this library with Eval or Data Binding in general?

Read validation message from external library

I created a Global Resource file for Error messages and I am attaching the associated message to the validator control as following.
<asp:RequiredFieldValidator ID="RVTest"
CssClass="ErrorMessage" runat="server"
ErrorMessage="<%$ Resources:ErrorMessage, RequiredFieldTestKey %>"
ControlToValidate="ReqFldTestTextbox"> </asp:RequiredFieldValidator>
This is working fine.
Now I am planning to move all the Global Resources to a different library let say MyResourceLibrary and created a resource file with name ErrorMessage. I have added the reference of the library to my ASP.NET project and trying to acces the message from my library as follwong.
<asp:RequiredFieldValidator ID="RVTest"
CssClass="ErrorMessage" runat="server"
ErrorMessage="<%$ Resources: MyResourceLibrary.ErrorMessage, RequiredFieldTestKey %>"
ControlToValidate="ReqFldTestTextbox"> </asp:RequiredFieldValidator>
But this is not working.
If your resource file is named the same as the page you are on (i.e. Default.aspx.resx)
You can add meta:resourcekey as so:
<asp:RequiredFieldValidator ID="RVTest"
CssClass="ErrorMessage" runat="server"
meta:resourcekey="RequiredFieldTestKey"
ControlToValidate="ReqFldTestTextbox"> </asp:RequiredFieldValidator>
Finally, I did this using server side code like follwong.
RVTest.ErrorMessage =MyLibrary.ValidationMessages.RequiredField;

Categories