Getting text from the Resource file in asp.net 4 - asp.net

i have an asp.net webpage, which contains a label, now i want to get the text property of the label from the localresource file, i have a .resx file under App_LocalResources folder, which contains Name as UserNameLabel.Text and Value as User Name now in my aspx file i am using Label control like this
<asp:Label ID="UserNameLabel" runat="server" resourcekey="UserNameLabel"></asp:Label>
but i cannot get the text on the Label, can anyone tell me the correct way to add the Text Property from resource file

First you need to create appropriate structure inside web project. In this case I will be using Default.aspx:
Take notice I have placed Default.aspx.resx file inside App_LocalResources.
Next enter new item inside Default.aspx.resx like this:
The important thing is you need to set Text property (UserNameLabel.Text)
And finally here is aspx code:
<asp:Label ID="UserNameLabel" runat="server" meta:resourcekey="UserNameLabel"></asp:Label>
I have used meta:resourcekey to link to appropriate resource key.

Related

Passing a Parameter When Clicking an ASP Literal

I am working on an ASP.NET web forms project. I have two main web pages, FirstPage and SecondPage, each of which have a .aspx file and a .aspx.cs file. A drop down menu is on top of each page, and the values are taken from an Oracle database. Switching between the two pages is done by clicking on an asp literal. When Firstpage.aspx is loaded, user selects a value from the drop down list. The corresponding code in the .aspx file looks like this:
<a href="SecondPage.aspx">
<asp:Literal runat="server" id="secondpagetab" EnableViewState="false"/>
</a>
What I need to do is to display that selected value as the default value of the drop down whenever the pages are switched. How do I do this? The value is loaded in the FirstPage.aspx.cs file. How can I pass it to the SecondPage.aspx file? Can I pass with the href tag?

Same piece of code works on one page & doesnt work on other Page, asp.net

nesting of image inside a LinkButton shows image on one page & doesn't show image on another page below are two sample code from two different pages in the same root director.It works fine on one page but on the other page is doesn't show any download image rather shows the text download in place of image.
I have done troubleshooting for sometime and replace the code also but it doesnt show download image for any reason on the second page..
<asp:LinkButton ID="lnkbtnDownload" runat="server" onclick="lnkbtnDownload_Click" meta:resourcekey="lnkbtnDownloadResource1">
<asp:Image ID="imgDownload" runat="server" ImageUrl="~/images/download.png" meta:resourcekey="imgDownloadResource1" />
</asp:LinkButton>
<asp:LinkButton ID="lnkbtnDownload" runat="server" onclick="lnkbtnDownload_Click" meta:resourcekey="lnkbtnDownloadResource1">
<asp:Image ID="imgDownload" runat="server" ImageUrl="~/images/download.png" meta:resourcekey="imgDownloadResource1" />
</asp:LinkButton>
HTML OUTPUT
HTML for above two code sample render as below
<img alt="Download" src="images/download.png" id="MainContent_imgDownload">
Download
Both Pages are in the same root directory...
The problem probably comes from a discrepancy in your resource files or missing a resource file completely for the second page. Obviously you have one for the first, but possibly not for the other which has different naming.
If you are using meta:resourcekey, there are some things you have to considerate.
Make sure that your local resource files meet the following criteria:
They are in an App_LocalResources folder.
The base name matches the page name.
For example, if you are working with the page named Default.aspx, the
resource files are named Default.aspx.resx (for the default
resources), Default.aspx.es.resx, Default.aspx.es-mx.resx, and so on.
The resources in the file use the naming convention
resourcekey."property". For example, key name Button1."Text".
Source: MSDN

How does a local resource file look like?

I can't seem to find a detailed example of how a local resource file looks like and its content
I have been reading up on this link
http://msdn.microsoft.com/en-us/library/ms227427.aspx
I want to use implicit localization with local resources.
So if I have a page: Products.aspx and want to create a default resource file then I need to create Products.aspx.resx and placed in App_LocalResources folder?
What does it contain say for example I want to change the values for a button control with ID="btnSubmit" ?
Is it like:
btnSubmit.Text = "Click here" ?
Yes you would place the "Products.aspx.resx" in the App_LocalResources.
You would then create an item in the resource file where:
Name = btnSubmit.text
Value = Click Here
your aspx page would have the button with the resource key defined:
<asp:Button runat="server" ID="btnSubmit" meta:resourcekey="btnSubmit" />

How to provide translations for items in a ASP.Net custom web control (.ascx)?

How do I go about providing localised text for items in a custom web control? I had thought that I just need to add meta.resourcekey tags to the control items and then define and fill some resource files called mycontrol.ascx.[lang].resx but that doesn't seem to work.
e.g.
MyControl.ascx
<asp:Label ID="Label1" meta:resourcekey="Label1" runat="server" Text="Oops!"></asp:Label>
MyControl.ascx.de.resx
Label1.Text Donner und Blitzen!
You dont need to add meta tags.
Have this in your resource file (MyControl.ascx.de.resx) which will be located in App_LocalResources:
Name Value
SomeName Oops!
Then in your User Control:
<asp:Label ID="Label1" meta:resourcekey="Label1" runat="server">
<%=GetLocalResourceObject("SomeName") %>
</asp:Label>
That helper method is part of the System.Web.UI.TemplateControl namespace.
you could use global resource files for this, here is a link that may help

Can i put special characters like ® in the .resx file?

I have created a ProductResources.resx file. The name and value fields contain the ® character. THis is the R with a circle around it. It is a reg trademark. Obviously this needs to be there. An the resource file itself gives a red circle with a question mark inside it. Please help.
Depending on the encoding you declare for the resx XML, you can probably use one of the following:
® (iso-8859-1 or utf-8)
™ (unicode)
here's what you must do:
add a app_local_resources folder(right click on your project --> add asp.net folder)
name the resource folder as the following: PageName.aspx.resx
add the button name and property ( eg. Button1.Text) in the resource file name field
copy (by just highlight the registered trade mark from any source and copy it, paste in the value field from here
go to your button and add the following such as:
asp:Button ID="Button1" runat="server" Text="" onclick="Button1_Click" meta:resourcekey="Button1"
alt text http://img7.imageshack.us/img7/2108/59469768.jpg
hope this is what you want :)

Resources