Quick Ajax example using Prototype.js
by Steven Ng on March 20, 2007
Overview
UPDATE: Please visit my updated article appropriately named “Quick Ajax Example Using jQuery“.
This example will explain how to use the Prototype.js Javascript library to perform a simple Ajax call from an html page to an ASP.NET page with dynamic info. In this example the dynamic ASP.NET page is getting the current date and time, in a real-world example an ASP.NET page could parse RSS feeds, show stocks quotes, lookup weather, etc.
Requirements
Let’s code!
- Create a web form called
ShowTime.aspx(ShowTime.cswill be auto created) in Visual Studio 2005 and add a label control as shown below: - Open up
ShowTime.csand declare a DateTime type variable and set it to display the current date and time:DateTime dtRightNow = DateTime.Now; - Inside the
Page_Loadset the page to not cache itself with the following code:Response.CacheControl = "no-cache"; - Assign the current date and time to the
lblDateTimecontrol:this.lblDateTime.Text = String.Format("{0:d}", dtRightNow) + " @ " + String.Format("{0:T}", dtRightNow); - Now let’s create
ShowTime.html. This page will call thePrototype.jsand contain some Javascript to make an Ajax call toShowTime.aspx - Between the
tags, reference thePrototype.jsJavascript library like this:. - Now we will write the Javascript that uses
Ajax.UpdaterfromPrototype.js. Below is the code that declares the page containing the dynamic content and the element to output the data when the link is clicked (you could also include the snippet below in a seperate Javascript file). - We're almost done! Just need to ad add a few more things between the
tags. We will add a link with an ID called showtimelink and awith the ID of output.; ; - That's it! View the demo.
Download
- ShowTime.zip (18kb)
8 comments
This does not work in Firefox.
by Scott on June 14, 2007 at 4:15 pm. #
Eiii! thanx! it does work =) i rather use this option than using those horrible update panel heheh
by Jerry Sosa on July 12, 2008 at 5:55 pm. #
it works on both, firefox and ie =)
by Jerry Sosa on July 12, 2008 at 5:58 pm. #
One last thing, it is possible to call an specific function?? can you email me ? LOL
Byee
by Jerry Sosa on July 12, 2008 at 6:01 pm. #
Thanks for the example.
Can you tell me why I cannot make this work if I try to do the exact same from an aspx page (instead of the html page), when I click the link nothing gets updated in the aspx page.
Thanks
by Thomas on December 5, 2008 at 7:47 am. #
@Scott
Yes it works (http://dev.hostreaction.net/examples/showtime.html), but the author’s article is all jacked up with step 7 missing half the instructions.
by Nealosis on December 13, 2008 at 10:39 pm. #
[...] 2 years ago I posted an article called “Quick Ajax Example Using Prototype.js” and some visitors commented that my instructions weren’t clear so I just wanted to [...]
by Steven Ng » Blog Archive » Quick Ajax Example Using jQuery on March 1, 2009 at 9:30 pm. #
Hi Everyone, I apologize if this article may have been confusing for some of you. I have an updated article explaining the same concept but using jQuery a more efficient javascript library. http://stevenng.net/2009/03/01/quick-ajax-example-using-jquery/
by Steve on March 1, 2009 at 9:38 pm. #