Globalization difference in inline tags in ASP.NET - asp.net

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.

Related

client id mode on 2 identical id's

I have an control1.ascx page
and control2.ascx page
both of them have this element:
<asp:TextBox runat="server" ID="txt_name" ClientIDMode="Static"></asp:TextBox>
page.aspx contains both of the controls.
so now the page contains input type text with the id "txt_name" X2.
I am wondering how it is working? can someone explain?
Ideally, you want to use ClientIDMode="Static" only if you are sure that no other control has same name in the page.
For example, you really want to access the ServerControl from external javascript file (althought it is not a good design).
If you are not sure, you want to use Predictable.
As your are using ClientIDMode="Static" thus control's id will be rendered exactly as it is.
ID will be rendered directly. See MSDN docs, this Blog is a good read.
ASP.Net 4+ supports various modes to generate ClientIDs for controls.
Here is a reference for MSDN on ClientID and its' generation modes: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid(v=vs.100).aspx
To see how ClientID generation works, you may want to refer to this link :
http://www.codeproject.com/Articles/108887/Client-Ids-Generation-with-ASP-NET-4-0
(It has visual explanations on how the ClientID generation works in different cases)
Hope this helps.

ASP.Net - Naming Containers and ClientIDMode="Static"

MSDN documentation states with regard to the ClientIdMode:
Static The ClientID value is set to the value of the ID property. If the control is a naming container, the control is used as
the top of the hierarchy of naming containers for any controls that it
contains.
source: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientidmode%28v=vs.100%29.aspx
However, i am not finding the "top of the hierarchy" business to be the case. For example, I have a usercontrol:
<uc1:WidgetsListControl runat="server" id="WidgetsListControl" ClientIDMode="Static" />
For good measure, I set the clientidmode in the control source as well although I'm not sure which one is needed:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WidgetsListControl.ascx.cs" Inherits="WebApplication1.WidgetsListControl" EnableViewState="false" ClientIDMode="Static" %>
Within the user control, I have a textbox:
<asp:TextBox ID="testTextBox" runat="server" />
My expectation is that the text box would be named something like WidgetsListControl$testTextBox
However what I find upon view source is:
<input name="ctl00$MainContent$WidgetsListControl$testTextBox" id="testTextBox" type="text"/>
What am I missing? Is there a way to achieve what I'm looking for (shorter ids) without setting 'static' on every control within the user control?
EDIT:
Actually after looking closer, I am finding that the ID attribute is working as described in the MSDN - however the name attribute is still the full concatenation of the naming-container hierarchy.
Given a site of high complexity, the names and IDs of these controls start to take up the majority of the bandwidth (markup size). I can't seem to find any good workaround to slim down the markup in this regard.
You must have this control within a Panel or something with runat="server" and the custom control itself is a naming container for its children. If you want identifiers to be how you name them with no change, you'll want to use the Predictable (IIRC) client id mode.
Note: though looking at the documentation for Predictable this doesn't seem to be the case, I'm sure it's what I've always used to get 'clean' .NET identifiers in ASP.NET.
Further, if you want to apply the ClientIDMode globally, then specify it in the web.config file in the attribute of the <pages> element.
ClientIdMode="Static" does work as described by Microsoft.
Make sure your looking at the id in the generated source, not the name.
That's the mistake I made.

Explicit localization problem

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.

referencing appSettings from usercontrols

In my .ascx usercontrol i'm trying to dynamically generate links using a value i've stored in web.config.
link
and when i try running, i get a parser error
Literal expressions like '<%$appSettings.MYPATH %>' are not allowed. Use <asp:Literal runat="server" Text="<%$appSettings.MYPATH%>" /> instead.
I know i'm probably missing something relatively minor.
<%= ConfigurationManager.AppSettings["myKey"] %>
EDIT:Don't forget the =
link
should work (it at least does on the IIS server I use). (Unfortunately it's more verbose)
Try this instead
.ascx
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
in code behind
Literal1.Text = "link"
More accurate answer will be next:
Link
Use a colon instead of a dot and add runat="server":
link
The documentation isn't very clear on this point, but ASP.Net Expressions are for use within server tags. Thus, if you want to use one in a plain html tag, you must add runat="server" so that the tag is processed on the server where the expression will be evaluated.

Label Text Property and entities

The following asp label fails to be displayed in the browser, can someone please
tell me what I am doing wrong. I expect to see the value <abc> but instead
I get nothing.
<asp:Label ID="Label1" runat="server" Text="<abc>"></asp:Label>
By the way, I realize that I can accomplish the same thing doing the following:
<asp:label id="Message1" runat="server"> <abc> </asp:Label>
But that is not really what I am asking for, what I would like to know is if using a string such as "<abc>" in an attribute value for an asp elements is allowed or not. In other words, is this an ASP.Net bug or is this behavior by design and if it’s by design what’s the reason for such design?
Thank you very much.
Believe it or not, but you can include entities without escaping them, thus:
<asp:Label runat="server" ID="myLabel" Text="<abc>" />
This will render an <abc> tag.
Edit: OK, sorry, you want to display the brackets, not make a tag, of course..
Using entity references in the Text attribute will give the same result - an (invisible) <abc> tag - because they are translated when the tag is parsed server-side. What you must do is:
<asp:Label runat="server" ID="myLabel" Text="&lt;abc&gt;" />
This will give the desired result - the & entity reference will render an ampersand to the client. Followed by lt;, the result is a correct client-side entity reference (<). Which will render as <.
To answer you questions explicitly: Yes, using entity references in ASP.NET attributes is (obviously) OK, since it's an XML format. This is not really a 'decision' on Microsoft's part (and certainly not a bug) - i's simply XML.
The trick is realizing when the entity references are parsed (when the tag is parsed on the server), and what the resulting text is, which is what will be sent to the client.
Yes it's allowed of course. Label control's purpose is to show text and markup to client. And it's really useful I think. injected code is your responsibility.
The asp.net aspx parser will unescape the "<" and ">" to "<" and ">". It will generate something like this method:
[DebuggerNonUserCode]
private Label __BuildControlLabel1()
{
Label __ctrl = new Label();
base.Label1 = __ctrl;
__ctrl.ApplyStyleSheetSkin(this);
__ctrl.ID = "Label1";
__ctrl.Text = "<abc>";
return __ctrl;
}
If you wanted to write it in the text property you could double escape like "&lt;", but it is probably easier just to write it between start and end tags like you mention.
<asp:Label ...><abc></asp:Label>.

Resources