Quick Ajax example using Prototype.js

Posted by Steve on Mar 20, 2007 in ASP.NET, Javascript |

Overview

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!

  1. Create a web form called ShowTime.aspx (ShowTime.cs will be auto created) in Visual Studio 2005 and add a label control as shown below:
  2. Open up ShowTime.cs and declare a DateTime type variable and set it to display the current date and time:
    DateTime dtRightNow = DateTime.Now;
  3. Inside the Page_Load set the page to not cache itself with the following code:
    Response.CacheControl = "no-cache";
  4. Assign the current date and time to the lblDateTime control:
    this.lblDateTime.Text = String.Format("{0:d}", dtRightNow) + " @ " + String.Format("{0:T}", dtRightNow);
  5. Now let’s create ShowTime.html. This page will call the Prototype.js and contain some Javascript to make an Ajax call to ShowTime.aspx
  6. Between the tags, reference the Prototype.js Javascript library like this:
  7. 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 a
    with the ID of output.

    
    
    
    ;
    
    ;
  8. That’s it! View the demo.

Download

5 Comments

Scott
Jun 14, 2007 at 4:15 pm

This does not work in Firefox.


 
Jerry Sosa
Jul 12, 2008 at 5:55 pm

Eiii! thanx! it does work =) i rather use this option than using those horrible update panel heheh


 
Jerry Sosa
Jul 12, 2008 at 5:58 pm

it works on both, firefox and ie =)


 
Jerry Sosa
Jul 12, 2008 at 6:01 pm

One last thing, it is possible to call an specific function?? can you email me ? LOL

Byee


 
Thomas
Dec 5, 2008 at 7:47 am

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


 

Reply

Copyright © 2009 Steven Ng All rights reserved. Theme by Laptop Geek.