What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 37969 |  Welcome, Guest!   Register  Login
Home > Articles > JavaScript > How to use String.Format function of C# in Javascript and display multiple values of a resource file.

How to use String.Format function of C# in Javascript and display multiple values of a resource file.

Article posted by Niladri.Biswas on 5/19/2012 | Views: 2919 | Category: JavaScript | Level: Beginner | Points: 250 red flag


In this article we will look as how to use String.Format function of C# in JavaScript and display multiple values of a resource file.

Download


 Download source code for How to use String.Format function of C# in Javascript and display multiple values of a resource file.


Introduction

Recently,I had a requirement where I need to access various values of resource file in javascript. So I thought of sharing the same here. In this article, we will look as how to display the current date and time using the C# DateTime.Now property in conjunction with reading some texts from resource files and combining them altogether using the String.Format C# function.

Straight to code

Let us first create a resource file (name it as "Test.resx") which has the following Names and Values

Next, create a Test.aspx page and have a button control. And on the OnClientClick event invoke a "Test()" function

	<asp:Button ID="btnTest" runat="server" Text="Invoke" OnClientClick="Test()" />

Lastly, implement the "Test()" funciton as under

<script language="javascript" type="text/javascript">
function Test() {
var combinedResourceValue = '<%= string.Format("{0}: {1} {2}: {3}", Resources.Test.Value1, DateTime.Now.ToString("MM/dd/yyyy") ,Resources.Test.Value2,DateTime.Now.ToString("HH:mm:ss tt")) %>';
alert(combinedResourceValue);
}
</script>

We can invoke the C# functions within a set of <%= and %> template operators.The output is as under

N.B.~The way to read resource file in java script is presented in Read Resource File values in JavaScript

Conclusion

So in this short article we have seen as how to use C# function, Resource file values in JavaScript. Hope, this will be useful.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:6 year(s)
Home page:http://www.dotnetfunda.com
Member since:Monday, October 25, 2010
Level:Diamond
Status: [Member]
Biography:Lead Engineer at HCL Technologies Ltd., having 6 years of experience in IT field.
I love to explore new technologies and love challenges and try to help others as much as possible not only by coding but also by all possible means.
>> Write Response - Respond to this post and get points
Related Posts

This article explains the use of execCommand method used in Javascript that is used to execute a command in JavaScript.

I am going to explain about javascript code for Drag from ASP.Net ListBox and to Drop in the specific Cursor location of TextArea .

This is HTML application which is mainly used in e-Commerce applications where the online shopping is done. All the registered users who logs in can access this appliction by selecting the products they wanted. The code might look clumsy but just copy paste and enjoy the article !!!

While writing UI code, we must have came across scenaio where we need to get the asp:RadioButtonList value into JavaScript and validate some other field.

In this article I am going to show how to get value from asp.net controls like TextBox, RadioButtonList, DropDownList, CheckBoxes through JavaScript.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/23/2013 5:50:47 AM