ASP.NET Translate and Localize an WebForms App - c#

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.

Related

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.

In asp.net what is the difference between using asp.net web control and simple html imput control

In my asp.net web control form i am using two text box 1st is simple input html control and 2nd is asp.net input web control.
<form id="form1" runat="server">
Email: <input type="text" id="txt_email" name="txt_email" value="" /><br />
Email2: <asp:TextBox ID="txt_email2" runat="server"></asp:TextBox><br />
<asp:Button ID="btn_login" Name="btn_login" runat="server" Text="Button"
onclick="btn_login_Click" />
</form>
I need to know what is the difference using simple control and asp.net input control both of them pass the value to code behind after the form submit. can any one help me on this?
As defined in your example input type="text" won't even be visible to code-behind because it is missing runat="server" attribute.
If you do add it - there're still differences. ASP.NET TextBox is more advanced and in par with the rest of ASP.NET model (e.g. it has property .Text vs. .Value of an HtmlInput control, it has events and other properties).
But if you simple need to pass text information back to the server, either of them will do the job.
The biggest differences are that
the asp.net controls are rendered on the server, and thus they have more overhead on your server than using traditional controls - traditional controls (by default) are rendered once then basically reside on the client's browser, asp controls are persistent on the server side.
the asp controls can be accessed and worked with directly in the code behind files.
asp controls have some additional tags that can be used on their fields usually.
as was pointed out by #Yuriy-Galanter, how the value is accessed is slightly different.
The asp:Textbox renders HTML to the client/browser when the page request is made. Picture the ASP.NET control, in this case an asp:TextBox as a server side bit of code that knows to render a <input type="text"> HTML element when the request for the aspx page is made to the server.
The ASP.NET compiler, when parsing your aspx page just spits out the <input type="text"> HTML element you have for Email: and for Email2: the ASP.NET compiler knows that is a server control because of the runat="server" tag. So the ASP.NET compiler, having a reference to the ASP.NET assemblies on the server, reads the code for the <asp:TextBox> and knows to ultimately respond to the page request with an <input type="text" id="txt_email2" />
The server side controls are accessible in your code behind page. So is accessible in the code behind but the <input> element is not. Good for you to consider in your research at this point, that if you add runat="server" to your element, it is accessible in your code behind.

Remove <br /> tag from generated CheckboxList

I've never been a fan that checkbox lists generate so much markup, but that's besides the point. For various reasons I need to use RepeatLayout="Flow" It generates a <br /> tag between each checkbox. Outside of using something client side is there a way to not include that with a server side solution?
Add the attribute RepeatDirection="Horizontal" to the CheckBoxList tag in your HTML template.

Adding Div dynamically to my web page as per the contents in the table using ASP.Net

I want to display every individual record from table on my web page, if the records in table increases the number of div should also increase.
How do i achieve this using ASP.net.
Please if any one can guide me as i am new to ASP.Net
Thank you in advance
As stated earlier using MVC would be a nice approach but u will have to learn a bit to use it.
As per the current scenario you can use repeaters which are exactly designed for what you want to accomplish.
assuming your database looks something similar to this..
id int primary key
name nvarchar(50)
etc...
//suppose you have your database data in "dataFromDatabase"
now in your asp.net page just define a repeater like this
<div id="content">
<asp:repeater id="repeaterData" runat="server">
<HeaderTemplate>
<Table>
<tr>
<th>Id:</th>
<th>Name : </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<td><%#Eval("id")%></td>
<td><%#Eval("name")%></td>
</ItemTemplate>
<FooterTemplate>
</Table>
</FooterTemplate>
</repeater>
</div>
and in your .aspx.cs page
bind the repeater with your datasource
repeaterData.DataSource = dataFromDatabase
repeaterData.DataBind();
thats all you need to do to get it up and running...
and using GridView would be even more simpler you just need to drag that from ToolBox to your aspx file and select your data Source visually and violla you are done... no coding required.....
although you will have to do some coding for the edit, delete and some fancy stuff...
Hope it helps.
It sounds like you're looking to implement a repeater class.
EDIT
Here is some sample code per your request from the link mentioned above:
<asp:Repeater
DataMember="string"
DataSource="string"
DataSourceID="string"
EnableTheming="True|False"
EnableViewState="True|False"
ID="string"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnItemCommand="ItemCommand event handler"
OnItemCreated="ItemCreated event handler"
OnItemDataBound="ItemDataBound event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
runat="server"
Visible="True|False"
>
<AlternatingItemTemplate>
<!-- child controls -->
</AlternatingItemTemplate>
<FooterTemplate>
<!-- child controls -->
</FooterTemplate>
<HeaderTemplate>
<!-- child controls -->
</HeaderTemplate>
<ItemTemplate>
<!-- child controls -->
</ItemTemplate>
<SeparatorTemplate>
<!-- child controls -->
</SeparatorTemplate>
</asp:Repeater>
You can use gridview or repeater to solve your.
Gridview basic example
http://www.codeproject.com/Articles/23471/Editable-GridView-in-ASP-NET-2-0
Repeater basic example
http://www.w3schools.com/aspnet/aspnet_repeater.asp
If you're completely new to ASP.NET, I'd suggest working with the ASP.NET MVC Framework instead of with WebForms. Ultimately it's a matter of taste, but the MVC framework has been designed with the deliberate goal of making it easy to write good quality code, so if you're starting fresh anyway you might as well learn to do it well from the beginning.
The best place to start is probably at the official site of ASP.NET MVC. They have a good "getting started"-guide, and lots and lots of tutorials on levels from absolute beginner to advanced web developing.
And a side note to all the WebForms fans out there: I'm definitely not saying you can't write good quality code with WebForms - however, if you look around the internet most examples you find are poorly constructed and full of antipatterns. Starting with ASP.NET MVC Framework yields a higher chance of the web resources you find demonstrating good practices, rather than quick hacks.
or:
for(int i= 0;i<10i++){
label.text="<div>"+something+"</div>"+label.text
}

Categories