Click here to Skip to main content
6,290,721 members and growing! (13,079 online)
Email Password   helpLost your password?
Web Development » Ajax and Atlas » General     Intermediate

AJAX for ASP.NET

By Steven A Bristol

An easy way to use AJAX with ASP.NET.
C#, Javascript, XML, HTML, Windows, .NET, ASP.NET, Visual Studio, WebForms, Ajax, Dev
Posted:28 Jun 2005
Views:124,824
Bookmarked:97 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
21 votes for this article.
Popularity: 4.32 Rating: 3.27 out of 5
3 votes, 14.3%
1
2 votes, 9.5%
2
4 votes, 19.0%
3
6 votes, 28.6%
4
6 votes, 28.6%
5

Introduction - What is AJAX?

AJAX, or Asynchronous JavaScript and XML, is a fancy way to use JavaScript and XML to communicate with a web server without refreshing the web page. You can visit this web page for more information about AJAX; it has a good list of links.

Why use AJAX?

There are a couple of reasons to use AJAX in lieu of the traditional form submission. The first is that it is very light weight: instead of sending all of the form information to the server and getting all of the rendered HTML back, simply send the data the server needs to process and get back only what the client needs to process. Light weight means fast. The second reason to use AJAX is because (as the logo in the link above makes clear) AJAX is cool.

Using AJAX with ASP.NET

Even though AJAX is a client side technology that uses JavaScript, the client-server interaction is very important.

Adam Vandenberg has put together a nice JavaScript wrapper for AJAX. His wrapper even does caching, although that will not be discussed here. His wrapper is used in the code examples and project files.

Using the code

There are four parts of the code that are important to look at:

  1. The HTML

    There are two form elements that will interact with AJAX. The input button "btn1" will invoke the AJAX code, which will make a server call and fill the select element "select1".

    <select id="select1"></select>
    <input id="btn1" value="Fill Select" type="button" 
    onclick="getOptions();">
  2. The JavaScript that calls the AJAX.

    The function getOptions() will do the main work.

    // Create the Request object (the AJAX wrapper)
    
    var request = new Request();
    // Change this to fit your environment
    
    var url = "http://localhost/ajax/";
    function getOptions()
    {
        // Call the AJAX
    
        // Notice the second parameter is actually a function to handle the 
    // response
    request.GetNoCache(url + "requests/getOptions.aspx", function(result) { if (result.readyState!=ReadyState.Complete) return; if (result.status==HttpStatus.OK && result.responseText != "") { // If the request was successfull and returned data var vals = result.responseText.split("~"); for (i=0; i<vals.length; i++) { var pair = vals[i].split("|"); var op = new Option(pair[1], pair[0], false, false); var sel = document.getElementById("select1"); sel.options[sel.length] = op; } alert("Remember that the new values in form" + " element 'select1' are not in viewstate." + " Code appropriately."); } else { // Handle the failure condition alert('Get options failed.'); } } ) }
  3. The ASPX file.

    The important thing here is that the aspx file only returns the string (XML) data from the code behind.

    <%@ Page language="c#"
                Codebehind="getOptions.aspx.cs"
                AutoEventWireup="false"
                Inherits="ajax.requests.getOptions" %>
    <%=result%>
  4. The code behind.
    protected string result = string.Empty;
    private void Page_Load(object sender, System.EventArgs e)
    {
        for (int i=0; i<10; i++)
        {
            result += i.ToString() + "|option " + i.ToString() + "~";
        }
        // to drop the last '~'
    
        result = result.Substring(0, result.Length - 1); 
    }

What's Next

As you can see, it is very easy to have a lightweight AJAX component interact with ASP.NET. I know what you are thinking: "Steve, wait a minute: I thought you had to use XML to use AJAX?" From the example, you can see that the string being returned is a pipe and tilde delimited string. You do not have to use XML. There are a lot of places where XML would be very useful, but using this implementation it is not required.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Steven A Bristol


Member

Occupation: Web Developer
Location: United States United States

Other popular Ajax and Atlas articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
GeneralYou can see PinmemberChiranjibC2:28 7 Feb '07  
GeneralRe: You can see PinmemberIslam91:53 4 Nov '07  
GeneralHav you tried... PinmemberDoodie Woddy Doodlechips3:00 24 Jan '07  
GeneralTurn ASP.NET controls into AJAXenabled controls PinmemberOmegaCD21:45 8 Jul '06  
GeneralRe: Turn ASP.NET controls into AJAXenabled controls PinmemberSteven A Bristol3:33 9 Jul '06  
GeneralUse the Ajax.NET library, not any of the advertisement messages trolls have left here. PinmemberSteven A Bristol13:24 27 Jun '06  
GeneralIs AJAX all about js PinmemberSyed Moshiur Murshed5:32 20 May '06  
Generalrequest is null or not an object PinmemberShri Pal23:03 31 Mar '06  
GeneralRe: request is null or not an object PinmemberShri Pal23:08 31 Mar '06  
GeneralRe: request is null or not an object Pinmemberzhongguosou17:10 2 May '06  
GeneralRe: request is null or not an object Pinmemberzhongguosou17:42 2 May '06  
GeneralRe: request is null or not an object PinmemberDiamond110:16 21 Aug '06  
Generalthank you Pinmemberminhhai_cd@yahoo.com22:34 2 Mar '06  
NewszumiPage for ASP.NET PinmemberAmir L.14:01 21 Dec '05  
GeneralComfortASP.NET Pinmemberdazei7:39 22 Sep '05  
GeneralThe code presented is very amateurish PinmemberDmitri Kirillov7:12 10 Jul '05  
GeneralAspnet Newbies here, need help open project. PinsussKhmerKid23:16 30 Jun '05  
GeneralRe: Aspnet Newbies here, need help open project. Fixed Pinmemberkhmerkidnow23:38 30 Jun '05  
Generalajax.net - The free library for .net (c#) Pinmemberploufs3:12 29 Jun '05  
GeneralRe: ajax.net - The free library for .net (c#) PinmemberJohn Rayner13:12 17 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Jun 2005
Editor: Chris Maunder
Copyright 2005 by Steven A Bristol
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project