For making an existing site to multilingual, I follow globalization technique, I have created 3 resource files for different content like label & header text file, grid header text file etc...
Now I have two keys in different resource files i.e Email and Message, at a third place I want to show both these keyword together i.e Email message. Do I need to create third key or I can concatenate already existing keys. Currently I am using below two codes
For showing directly on page:
HttpContext.GetGlobalResourceObject("ResourceLabelButton", "Email")
For showing as text of any control like Textbox, label:
Text ="<%$ Resources:ResourceContent, Email %>"
I can concatenate two resource string on .cs page but it will increase timeline, so please suggest so I can only change on .aspx pages.
Thanks in advance
ASP.NET tag attribute values that use <%$ Something: Something Else %> are of a special syntax called ASP.NET Expressions. Using them as attribute values are pretty much all-or-nothing; there's no way to add any code into the ASPX file to manipulate what those expressions evaluate to. You'll have to do this in the code-behind.
You should create a separate resource entry for "Email Message" instead of concatenating "Email" and "Message". To understand why, look at Spanish:
Email = Correo electrónico
Message = Mensaje
Simple concatenation gives you Correo electrónico Mensaje. This is wrong. The correct translation is:
Email Message = Mensaje de correo electrónico
Notice that in the translation, the order of "Email" and "Message" has been reversed, and a new word (de, which means "of") has been inserted between them.
On the other hand, look at the translations for "New Message" and "Last Message":
New Message = Mensaje nuevo
Last Message = Último mensaje
As you can see, there are complex rules that govern when to reverse the order of words and when to insert de. These rules, of course, are different in other languages.
So in general, you should never concatenate localized words or phrases to form a longer phrase.
Always provide a separate resource entry for every phrase.
I was having the same issue, and I solved it by using this option instead:
<asp:Label id="lbl1" runat="server" Text="Email: <%= HttpContext.GetGlobalResourceObject("ResourceContent", "Email") %>">
For local Resources, use the GetLocalResourceObject method instead of GetGlobalResourceObject
If this value is being used very often make a third key for the same, else concatenate at runtime
Try with the below code
<asp:Label ID="lblNameAndNos" runat="server">
<%= ""+ Resources.Resources.Name + "/" + Resources.Resources.Nos %>
</asp:Label>
Instead of using inside Text property use the above way
Related
I have this problem: From a database, held up a string, which contains HTML mixed with C# code. I wish I could run correctly both codes on my page .aspx.
e.g.
in my .aspx:
<div><%= Model.repo.getCode() %></div>
and the getCode() method give me this:
<div id="secondDiv"><p><%= Model.Person.Name %></p></div>
so I want the final html file look like:
<div><div id="secondDiv"><p>Jhon</p></div></div>
any suggestion?
There may be direct way to bind such value,
But if you could store String.Formatable into database then it would be easy to bind the data needed.
Using String.Format you achieve like,
returned string from Model.repo.getCode() (see curly braces)
"<div id="secondDiv"><p>{0}</p></div>";
And in ASP code,
<div><%= string.format(Model.repo.getCode(),Model.Person.Name) %></div>
Take a look at this project as it helped me with a similar problem: https://github.com/formosatek/dotliquid Basically you can bind whatever objects to a template and that template can call properties of you objects and even use conditional logic and loops.
I'm localizing an ASP.NET web site. Usually to localize text in an .aspx page I just use
<%= Resources.ResourceFile.ResourceName %>
For asp.net controls, this won't work. I have to use the syntax
<%$ Resources:ResourceFile, ResourceName %>
However, if I have a button and localize the Text property that way, but add any additional characters after it, the localization breaks and it shows as plaintext.
So Text="<%$ Resources:ResourceFile, ResourceName %> »" displays as
<%$ Resources:ResourceFile, ResourceName %> »
I'm sure there is a valid reason for this, I just can't find the explanation on MSDN on how the Text property evaluates this. I'm not even 100% sure on what the <%$ actually does.
What's happening is that ASP.net is invoking an Expression Builder. What effectively happens here is that rather than the ASP.net compiler translating your:
<asp:AControlWithATextProperty runat="server" Text="Some Text">
to:
AControlWithATextProperty ctl1 = new AControlWithATextProperty();
ctl1.Text = "Some Text";
When it transforms the markup in the .aspx file into a .cs file combined with the code-behind, it actually does something similar to this:
<asp:AControlWithATextProperty runat="server" Text="<%$ Resources:ResourceFile, ResourceName %>">
Becomes:
AControlWithATextProperty ctl1 = new AControlWithATextProperty();
ctl1.Text = ResourceExpressionBuilder.EvaluateExpression("ResourceFile, Resourcename");
It would seem that the asp.net compiler can't handle concatenating the content of the <%$ %> tags with any further text in the property from markup. Either a bug, or by design. i.e. You don't end up with ctl1.Text = ResourceExpressionBuilder.EvaluateExpression("ResourceFile, Resourcename") + "»".
You can read more about the ResourceExpressionBuilder on msdn, ExpressionBuilder in general, or if you really want to; an implementation of one for localisation (database backed, hence the fact that I didn't use the ResourceExpressionBuilder) on my blog (3 parts).
I have a requirement that user can input HTML tags in the ASP.NET TextBox. The value of the textbox will be saved in the database and then we need to show it
on some other page what he had entered. SO to do so I set the ValidateRequest="false" on the Page directive.
Now the problem is that when user input somthing like :
<script> window.location = 'http://www.xyz.com'; </script>
Now its values saved in the database, but when I am showing its value in some other page It redirects me to "http://www.xyz.com" which is obvious
as the javascript catches it. But I need to find a solution as I need to show exactly what he had entered.
I am thinking of Server.HtmlEncode. Can you guide me to a direction for my requirement
Always always always encode the input from the user and then and only then persist in your database. You can achieve this easily by doing
Server.HtmlEncode(userinput)
Now, when it come time to display the content to the user decode the user input and put it on the screen:
Server.HtmlDecode(userinput)
You need to encode all of the input before you output it back to the user and you could consider implementing a whitelist based approach to what kind of HTML you allow a user to submit.
I suggest a whitelist approach because it's much easier to write rules to allow p,br,em,strong,a (for example) rather than to try and identify every kind of malicious input and blacklist them.
Possibly consider using something like MarkDown (as used on StackOverflow) instead of allowing plain HTML?
You need to escape some characters during generating the HTML: '<' -> <, '>' -> >, '&' -> &. This way you get displayed exactly what the user entered, otherwise the HTML parser would possibly recognize HTML tags and execute them.
Have you tried using HTMLEncode on all of your inputs? I personally use the Telerik RadEditor that escapes the characters before submitting them... that way the system doesn't barf on exceptions.
Here's an SO question along the same lines.
You should have a look at the HTML tags you do not want to support because of vulnerabilities as the one you described, such as
script
img
iframe
applet
object
embed
form, button, input
and replace the leading "<" by "& lt;".
Also replace < /body> and < /html>
HTML editors such as CKEditor allow you to require well-formed XHTML, and define tags to be excluded from input.
hi guys i am using this code in my aspx page
<%for (int i=o;i<5;i++)
{%>
<asp: link button id=i text=i/>
<%}%>
at it's producing five link buttons like
i i i i i
but i just want five link buttons like that
1 2 3 4 5
with id=1,2,3,4,5 respectively
how can I can implement that
Put a placeholder in the page where you want the links:
<asp:PlaceHolder ID="LinkContainer" runat="server" />
In the Page_Load method in code behind, you put the links in the placeholder:
for (int i = 1; i <= 5; i++) {
LinkButton link = new LinkButton();
link.ID = "Link" + i.ToString();
link.Text = i.ToString();
LinkContainer.Controls.Add(link);
}
This way the controls will be created early in the page cycle, and it's possible to hook up server side events to them if you like.
(Note that I added a prefix to the id. Having an id that is only digits can cause problems in some situations.)
This isn't exactly the best way to do things in ASP.Net. Dynamic controls have many gotchas, and you just found one of them. This code is a little better (make sure the page calls DataBind() at some point):
<%for (int i=0;i<5;i++)
{%>
<asp:LinkButton id="<%# i%>" runat="server" text="<%#i%>" />
<%}%>
but you'll find that events and changes don't wire up the way you'd expect them to, if it works at all. With only five of them, it's better to just list them out by hand, and things will be much easier down the road. Or, if they came from a datasource somewhere use a repeater and create them that way.
Really, you don't want to mix code into your aspx markup at all if you can help it. Leave it for the code behind!
You need to be careful how you actually render this. Note that HTML ids that start with a number are illegal according to the specification. I would prepend some alphabetic character to the id value to be sure that it is both legal HTML and that the control id generated is legal for ASP.NET. It's unclear to me whether it's legal or not to use just a numeric value for a control id. Note that ASP.NET control id characters must be alphanumeric or an underscore.
HTML Reference:
ID and NAME tokens must begin with a
letter ([A-Za-z]) and may be followed
by any number of letters, digits
([0-9]), hyphens ("-"), underscores
("_"), colons (":"), and periods
(".").
ASP.NET Reference
Note: Only combinations of
alphanumeric characters and the
underscore character ( _ ) are valid
values for this property. Including
spaces or other invalid characters
will cause an ASP.NET page parser
error.
YOu need to actually print out the value of I on the server side.
<%for (int i=o;i<5;i++) {%>
<ASP:LinkButton Id="<%# i%>" Text="<%# i %>" />
<%}%>
I have a label on a page which gets localized text through the meta:resourcekey attribute. The issue I have is that I want it to display different text depending on which view of a multiview they're on.
I tried adding the attribute though label.Attributes.Add("meta:resourcekey", "label"), but that doesn't seem to load any text. I tried it on PreRender, and same deal. The attribute appears when I look at the source, but no text is displayed.
Is this possible to do? The other option is to have 2 labels and change the visibility on page load, but that seems like the less elegant solution.
Thanks.
I think what you want for programmatic localisation in code behind is as simple as this:
ctrl.Text = (string)GetLocalResourceObject(“myCtrlKey.Text”);
ctrl.AnotherAttribute = (string)GetLocalResourceObject(“myCtrlKey.AnotherAttribute”);
Using LocalResource means that for a page called MyPage.aspx, you have created a resource file called MyPage.aspx.resx and/or MyPage.aspx.{culturename}.resx in the special directory App_LocalResource.
If you like Global Resources instead of local, use the special directory App_GlobalResource
to hold a resource file called MyResourceFileName.resx and call:
ctrl.Text= (string)GetGlobalResourceObject(“MyResourceFileName”, “myGlobalKey”);
copied from a blog about localization in the code behind
--
PS the reason that Attributes.Add("meta:resourcekey", "label") doesn't work is that "meta:resourcekey" isn't a real attribute and its use in the aspx is not really valid aspx markup - rather it's a preprocessing directive that causes the compiler to turn it into a longer list of attributes name/value pairs, based on what you've put in your resource file.
The approach of trying to assign a meta:resourcekey attribute will not work simply because they are treated specially by the page parser, and replaced before the page lifecycle code even really begins.
But meta:resourcekey is basically a declarative replacement for the code equivalent of accessing local resource files. In other words:
<asp:Label ID="MyLabel" meta:resource-key="MyResourceKey" />
is equivalent to:
<asp:Label ID="MyLabel" Text="<%$ Resources: myResXFile, MyResourceKey %>" />
is equivalent to the code:
MyLabel.Text = Resources.MyResXFile.MyResourceKey;
It looks like you're already dealing with your label in the code if you're trying to assign attributes to it. Why not set it's value in the code?