I'm looking at an ASP.NET application which makes heavy use of meta:resourcekey which seem to be connected to the resx files.
This is an area that seeems to have completely passed me by. Does anyone have any guidance on the benefits and purpose of this approach and best practices?
The meta:resourcekey syntax allows you use declarative syntax for Implicit Resource expressions. This is used when localizing a site for international use. As the Quickstarts (linked below) explain, these kind of expressions are linked to .resx files located in the App_LocalResources folder.
The benefit of this kind of expression is that it can use multiple properties for a single control which are defined in the .resx file instead of the ASPX itself.
For instance, take the label below:
<asp:Label ID="myLabel" runat="server" Text="This text is localizable" meta:resourcekey="myLabelResource1">
</asp:Label>
The resx file for this page could contain data for multiple properties attached to the label such as:
<data name="myLabelResource1.Font-Name">
<value xml:space="preserve">Default Font name</value>
</data>
<data name="myLabelResource1.Text">
<value xml:space="preserve">Text in default language.</value>
</data>
<data name="myLabelResource1.ToolTip">
<value>Tooltip in default language.</value>
</data>
The ASP.NET quickstarts provide a great primer if you want to understand the concept.
Related
Is it possible to localize DataPager control in asp.net?
Among the fields NextPreviousPagerField can be localized since it offers properties such as FirstPageText and LastPageText. But I am having trouble localizing NumericPagerField. It basically produces the page numbers and I can't find any way of localizing those.
I have tried changing the culture of current thread, but it didn't work.
There are also properties PreviousPageText and NextPageText in NumericPagerField control that can be localized as follows (Assuming you are using Resources):
<asp:NumericPagerField
PreviousPageText="<%$Resources:TestSiteResources, PreviousPageText %>"
NextPageText="<%$Resources:TestSiteResources, NextPageText %>"
... />
Also note that:
https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.numericpagerfield.nextpagetext#remarks
The value of this property is stored in view state. It can be saved
automatically to a resource file by using a designer tool. For more
information, see LocalizableAttribute and Globalization and
Localization.
So, I'm new to ASP.NET and website development in general. I've run into a problem using data binding to an XML file to build a site map for an ASP.NET application. Here's the first portion of the sitemap:
<Privo>
<child display="Current Projects">
<child display="Amifostin">
<child display="Experiments">
<leaf>HTT</leaf>
<leaf>MTT</leaf>
<leaf>HPLC</leaf>
<leaf>UV-Spec</leaf>
</child>
And the data binding from the site.master file:
<DataBindings>
<asp:TreeNodeBinding DataMember="child" TextField="display" />
<asp:TreeNodeBinding DataMember="leaf" TextField="#InnerText" />
</DataBindings>
What'd I'd like to do it something like this:
<leaf url="ExperimentsView.aspx/HTT">HTT<leaf>
and
<asp:TreeNodeBinding DataMember="leaf" TextField="#InnnerText" NavigateUrl="url"/>
BUT, here's the problem: when I try to bind the NavigateUrl, the only thing I can do is bind a type of node to a url - meaning, every leaf will link the the same url. Is there a way to bind a field of the leaf nodes to a (unique) url, or will I have to make different DataMembers for each unique url?
Note: yes, I know about Web.sitemap. That's what I was using when the project lead told me that he wants to use XML data binding.
You will need to use the NavigateUrlField property to do this (see http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treenodebinding.navigateurlfield.aspx)
Is there any advantage/downfalls between using the inline write tag instead of the resource tag? Example:
<%=Resources.Site.SampleString %>
The resources tag (expression tag) as seen in any MSDN example:
<asp:Literal id="Literal1" runat="server" text="<%$ Resources:Site, SampleString %>" />
I find the first option far easier to use, and it has IntelliSense, but maybe it won't function the same?
These methods will function exactly the same. The latter simply calls first one; there is a reason why strongly-typed resources access code is being generated in the background. Therefore you can use whatever method you please.
By the way, there is also another way - by using meta:resourcekey attribute. So you should be able to write:
<asp:Literal id="Literal1" runat="server"
meta:resourcekey="SampleString" text="Default one" />
and it all should work exactly the same.
EDIT on implicit Localization.
What I forgot to mention, is that with meta:resourcekey certain conditions have to be met. The conditions are:
Values are taken from App_LocalResources, therefore related resource file need to exist
Related resource file name must be pagename.resx, for example: Default.aspx.resx, Default.aspx.es.resx
The resource file must contain keys in form of resourcekey.propertyname, for example SampleString.Text, SampleString.ID (although I wouldn't localize control ID's)
Because of how resources are generated, the key mentioned above must exist in invariant resource file (Default.aspx.resx), or it won't be localized.
I realised after some time that the <%=Resources.Site.SampleString %> method does not have designer support (which is understandable). This doesn't bother me, but it is a difference (for future readers).
So if you need or want designer support, the second option would be necessary.
I have an ASP.NET site which uses a 3rd party activeX control. I have to pass a few parameters to the OBJECT tag in the HTML page. If i hardcode these parameters into the HTML everything works.
I would like to place the parameters in my web.config with app settings "key/value" pairs.
My problem is i cannot read the app key setting in the HTML markup to succesfully pass them in as parameters. I can read them fine from server side code behind.
What's the correct way to read these settings in the client side HTML markup ?
Thanks
In addition to using <%=ConfigurationManager.AppSettings["MyAttribute"]%>, as others have noted, you can also use expression builders. The syntax is a little different. Instead of <%=...%> you use <%$ AppSettings: MyAttribute %>, like so:
<object id="myObjectID attr="<%$ AppSettings: MyAttribute %>" ...>
If you are just dumping an appSettings value directly into static HTML (as I presume you are in this example), these two approaches are identical for all practical purposes.
What is nice about expression builders, though, is that you can use them to declaratively assign appSettings values to Web control properties, something you cannot do with the <%=...%> syntax. That is, with expression builders you can do something like:
<asp:Label runat="server" ... Text="<%$ AppSettings: MyAttribute %>" />
Whereas you could not do:
<asp:Label runat="server" ... Text="<%=ConfigurationManager.AppSettings["MyAttribute"]%>" />
The following code:
<%$ AppSettings: MyAttribute %>
is not compatible with general HTML markup and JavaScript function! It's good for asp tag.
Whereas
<%=ConfigurationManager.AppSettings("MyAttribute")%>
really work in general HTML markup.
so
<%=ConfigurationManager.AppSettings("MyAttribute")%>
is my recommendation!
You can use the ConfigurationManager in you ASPX page. Then you can add in your OBJECT tag parameters:
Web.Config
</configuration>
<appSettings>
<add key="Setting" value="Value"/>
<appSettings>
</configuration>
ASPX
<object>
<param name="Setting" value="<%= System.Configuration.ConfigurationManager.AppSettings["Setting"] %>" />
</object>
I suggest you generate your OBJECT tag dynamically at run-time from the server. This way you can inject whatever parameters you read from the web.config file.
You have a few options. If you add the runat="server" attribute to your object tag, you can access it from your codebehind using its ID, and add attributes that way:
myObjectID.Attributes.Add("attrName", "value")
If you don't want to do that, you could use inline literals:
<object id="myObjectID attr="<%= ConfigurationManager.AppSettings("MyAttribute") %>" ...>
Either way should get the job done.
when trying to translate the confirmation message to Norwegian i get the following error:
Cannot have more than one binding on property 'OnClientClick' on 'System.Web.UI.WebControls.LinkButton'. Ensure that this property is not bound through an implicit expression, for example, using meta:resourcekey.
i use Explicit localization in the following manner:
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="<%# Resources: lnkMarkInvoicedResource.OnClientClick%>"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
here's the local resource file entry:
<data name="lnkMarkInvoicedResource.OnClientClick" xml:space="preserve">
<value>return confirm('Er du sikker?');</value>
if i remove the meta attribute i get the English text(default).
how do i get the Norwegian text appearing without resorting to using the code behind?
Update:
removing the meta attribute prevents the exception from occurring but the original problem still exists. I can't get the Norwegian text to show.
only the default English text shows.
Another Update:
I know this question is getting old but i still can't get the Norwegian text to display.
If anyone has some tips please post a response.
Looks like you're making the problem harder by inlining the onclick. Why not split it out to a separate line?
<script>
function markInvoiced()
{
return confirm('<%= Resources.SomehowGet.lnkMarkInvoicedResource.OnClientClick%>');
}
</script>
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="return markInvoiced();"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
And while we're looking at your code, you realize that you're essentially building an <a> tag, right? As such, why not just build an <a> and save yourself some grief?
And finally, next project why not ditch the built-in ASP.NET localization nighmare in favor of something sane like FairlyLocal, in which case you'd write this:
<a href="#" onclick="return confirm(<%=_"really?"%>) onserverclick="lnkMarkInvoiced_OnClick" runat="server">
<%=_("Mark Invoice")%>
</a>
Are you using the .NET resource manager and satellite assemblies to store your localized resources? It looks like you have hard-coded the alternative language in your markup, rather than storing it in a language-specific resources assembly...
.NET has some extremely rich localization and globalization capabilities. If you use them properly, localization should be a pretty automatic thing (assuming your client is providing their language code as part of the HTTP headers). Even if your client has not configured their browser with the appropriate language, it is still easy enough to manually change the UI culture via a user request (clicking a flag icon, configuring a setting, etc.)
This article might be helpful: ASP.NET Web Page Resources Overview
That meta tag is using implicit localization when you're using explicit localization in the OnClientClick. You will need to choose one method or the other. If you are to use explicit localization, you'll need to do the necessary work to set the proper culture info in your application in the code-behind.