how to use Local resource file in client side - c#

In my application I want to use a local resource file string on client side without any jquery and javascript etc.
Currently I'm using code behind but would like to use in client side
awec.Text= Localization.GetString("ReqLodgeName.Text", LocalResourceFile);
like this. How do I use this resource file on client side for ASP control like
<asp:Label Id="awec" runat="server" Text='I want to access here' />

Say you have the key "ReqLodgeName.Text" with value 'I want to access here' in your LocalResourceFile which is in the App_LocalResources folder, you may then use the meta:resourcekey attribute: in your label as follows to retrieve the text:
<asp:Label id="awec" runat="server" meta:resourcekey="ReqLodgeName" Text='I want to access here' />
Or may explicitly localize using a different syntax instead of meta:resourcekey:
<asp:Label id="awec" Text="<%$ Resources:WebResources, ReqLodgeName %>" />
where WebResources is the name of the resx file with the resources in the App_GlobalResources folder and ReqLodgeName is the key name that has the text 'I want to access here'.

You can use the following. Add public method to code behind:
public string MethodName(string RequiredResourceker)
{
//return resource depending on RequiredResourceker parameter
}
modify you client control as follows:
Text="<%=MethodName("RequiredKey") %>"

Related

ASP.NET Translate and Localize an WebForms App

I have a task of translating a big WebForms App (some 300 aspx pages). I will use conventional approach on translating ASP.NET apps (using Resource files) and I will use ZetaResource to make my life easy.
However, I have a small issue:
What can I do with free text existent in aspx code? Take this code for instance, how can I convert the text This is a List of records available in the Database into an ASP:Label server control in order to use the embedded Translation Mechanism?
<form id="form1" runat="server">
<div>
<br />
This is a List of records available in the Database<br />
<br />
<asp:Button ID="cmdButton" runat="server" Text="Button" meta:resourcekey="cmdButtonResource1" />
</div>
</form>
Imagine you have a resource file named YourResource and the item you want is String1, you can just do this for free text:
<%= (YourResource.String1) %>
You have two choices for localization: explicit and implicit.
For explicit localization, you would put the text into a resource file and display it on the web page with code like the following:
<asp:Localize ID="Localize1" Runat="server" Text="<%$ Resources:LocalizedText, ListOfRecordsInDatabase%>" />
For implicit localization, the code would look like more like this:
<asp:Localize ID="Localize1" Runat="server" meta:resourcekey="LocalizeResource1" Text="This is a List of records available in the Database" />
For these one-off texts that exist only in a single page, I usually use implicit localization. For text that can exist on many pages (common labels, buttons, etc.), I use explicit.
The implicit localization resource files can even be auto-generated for you, once you put them into a server control, like <asp:Localize> or <asp:Label>.
There is a great walkthrough given on the MSDN site called, "Walkthrough: Using Resources for Localization with ASP.NET". It will tell you how to auto-generate the resource files.

Changing BizForm properties Kentico

I am currently trying to have a bizform as part of my transformation as:
<cms:BizForm runat="server" ID="BizForm" FormName="YourBizFormCodeName" EnableViewState="false" FormDisplayText="Form submitted"/>
In the DocumentType/PageType I have a field that allows the user to enter whatever they want to display once the form has been submitted, so in theory I need to go about and change FormDisplayText to what has been provided.
I have tried using Eval("SubmitText") inside FormDisplayText, but it doesn't work.
Does anyone have a solution for this?
Thank you
Following code works fine for me (Kentico v8.1):
<cms:BizForm runat="server" ID="BizForm" FormName="test" EnableViewState="false"
FormDisplayText='<%# CMS.MacroEngine.MacroContext.CurrentResolver.ResolveMacros("{%CurrentDocument.SubmitText#%}") %>' />

Asp.net render control client id in custom attribute of input field

I have following html in my aspx page
<asp:Textbox id="myTextField" runat="server" cssclass="mycssclass" data-control-id="<%= search.ClientID %>"></asp:textbox>
<asp:Button ID="search" runat="server" Text="Search" />
the problem is "<%= search.ClientID %>" render as it is in the aspx file. i need to render the client id of control.
Thy this :
And call
this.DataBind(); in page_Load
(notice change in <%#)
<asp:Textbox id="myTextField" runat="server" cssclass="mycssclass" data-control-id="<%# search.ClientID %>"></asp:textbox>
Another (more convenient solution) is to use html elements which are not server side :
<input type='text' id="myTextField" runat="server" class="mycssclass" data-control-id="<%= search.ClientID %>" />
and then get it via Request.Form[...] ( via name attribute)
If you want to know what the client ID will be, then search and read about the setting "ClientIDMode" - setting it to Static, for example, will set the client-side ID in the DOM equal to the server side ID that you set. Just make sure have only one instance of that Control on your page otherwise you have more than one control with that ID, and that won't fly. If that control will be in a repeater, item template or any other "repeating" control, then add an index counter for the loop to dynamically alter then ID slightly.

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>

Finding element in aspx by property (not Id)

I have a that would result in some uncompilable code in the designer.cs file:
<div id="tabs-1-2-3"></div>
If I add a runat="server" property, my designer file won't compile for obvious reasons.
Is there any way to add an extra property that wil be used internally as the id?
If you don't have runat="server" then you can access it through the old school way.
<input type="text" id="text1" name="text1" />
then from server side use
Request["text1"]
for div:
The best way to do this would be some form of ajax, since your client side script would be able to read that contents and pass it to a server side method
Access in code behind isn't possiable without runat="server" attributes
Use an <asp:Panel /> which will turn into a <div> in the HTML page.

Categories