|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionHave an ASP.NET 1.1 site that you want to quickly add some AJAX functionality to? Could you use the new Microsoft AJAX release? Of course, but you could also use any of the popular JavaScript libraries available and do it yourself just as easily. In some cases, you may not want to use the new Microsoft AJAX. Luckily, there are plenty of other good (and easy) alternatives! This article will demonstrate how to use a popular JavaScript library (jQuery) to add AJAX capabilities to your application with a minimal amount of code. BackgroundSo, why wouldn't you upgrade to ASP.NET 2.0 and just use the new AJAX functionality provided by Microsoft? Well, if your site is deployed and working – you may not want (or be able to) upgrade to ASP.NET 2.0 as quickly as you would like to. You could, of course, use the new Microsoft AJAX functionality in your ASP.NET 1.1 application (as noted in this article), but just in case you don't want to – this article offers another alternative. Most of the popular JavaScript libraries available today have AJAX functionality built right in, and it's usually a snap to use. Some of the most popular JavaScript libraries are Prototype, Yahoo User Interface Library (YUI), and jQuery. jQuery happens to be my personal favorite, so that's what this sample will use. I could wax poetic on why I like it so much, but as that isn't the point of this article, I'll let you decide for yourself. Whichever one you choose, the process outlined in this article shouldn't vary all that much. With all of that out of the way, let's get started. This sample will utilize a popular JavaScript library called jQuery to provide almost everything we need to utilize AJAX in this sample. If you are unfamiliar with jQuery, it is an extremely lightweight JavaScript library that has changed the way that I write JavaScript. It's made the process a lot more enjoyable as well. Using the codeThe attached demo project includes both samples described here. To use it, unzip it to a directory on your system, and then create a Virtual Directory in IIS called "AJAXJQuerySample", and point it to the folder that contains the unzipped files. Next, just open the AJAXJQuerySample.csproj file! Pre-requisitesFirst, you'll need to download the latest version of the jQuery JavaScript library from here (version 1.1 at the time of this writing). While you're doing this, you might want to bookmark the jQuery site for a quick browsing later on. One of the reasons that this is my library of choice is because of the extremely active user base. The forums are very active, and questions are usually answered very quickly. There are also tons of "plug-ins" that other users have made for jQuery that do just about anything you can think of. Sample 1: Get the current date and time from the serverFor this first example, we'll be creating a simple page that grabs the current time from the server. To begin, we'll need to include the jQuery file in our "Default.aspx" page, like so: <script type="text/javascript" src="jquery-1.1.js">
Then, we'll use jQuery to setup the AJAX action that we want to happen when the user clicks on the "Get Server Time" link: $(function() {
//this code is executed when the page's onload event fires
$("a#runSample1").click(function() {
$.get("GetServerTime.aspx", function(response) {
alert(response);
});
});
});
In case you're wondering what you're looking at, let's go over it line by line. The first line is executed when the page's " $(function() {
Then, we look for the " $("a#runSample1").click(function() {
Now that we're bound to the link's " $.get("GetServerTime.aspx", function(response) {
alert(response);
});
Now, let's take a look at the "GetServerTime.aspx" page. For this page, we don't want any HTML to be output, so we'll remove all of the HTML and just leave the reference to the code-behind file. <%@ Page language="c#" Codebehind="GetServerTime.aspx.cs"
AutoEventWireup="false" Inherits="AJAXJQuerySample.GetServerTime" %>
(**Note: You could also point to an HttpHandler, but in the interest of brevity, this example will follow this method.) The code-behind file should have the following code: private void Page_Load(object sender, System.EventArgs e)
{
Response.Expires = -1;
//required to keep the page from being cached on the client's browser
Response.ContentType = "text/plain";
Response.Write(DateTime.Now.ToString());
Response.End();
}
The first thing we need to do is keep the page from being cached in the browser: Response.Expires = -1;
The response of a "GET" request is cached by default, so to make sure that our example brings back the current time each time it is clicked, this line is crucial. The next few lines change the content type to plain text, get the current time, and writes the output: Response.ContentType = "text/plain";
Response.Write(DateTime.Now.ToString());
Response.End();
That's it! Now, we can run our sample. Click the "Get Server Time" link, and we should see the current time displayed in an alert box:
So, let's move on to a more complex example: saving data back to the server using an AJAX call. Sample 2: Saving data to the serverAs you may have noticed, in the demo project for download, the "Default.aspx" page does not have the " Save the plug-in code into a file named "form.js", and include it in the "Default.aspx" page. Here is the additional HTML that we'll be adding to our "Default.aspx" file: <div style="width:350px">
<div style="background:#CCC"> <a href="#" id="editName">Edit</a></div>
<div id="divView"><asp:literal id="litName" runat="server"/></div>
<div id="divEdit" style="display:none"></div>
</div>
The literal control $("a#editName").click(function() {
$.get("ChangeName.aspx", function(response) {
$("div#divEdit").html(response).show();
$("div#divView").hide();
var options = {
method: 'POST',
url: 'ChangeName.aspx',
after: function(response) {
$("div#divView").html(response).show();
$("div#divEdit").empty().hide();
$("a#editName").show();
}
};
//bind to form's onsubmit event
$("form#ChangeName").ajaxForm(options);
//hide the "edit" link
$("a#editName").hide();
//wire up the cancel link
$("a#lnkCancel").click(function() {
$("div#divView").show();
$("div#divEdit").empty().hide();
});
});
});
The first block of code above binds to the " $("a#editName").click(function() {
$.get("ChangeName.aspx", function(response) {
$("div#divEdit").html(response).show();
$("div#divView").hide();
This last example shows one of the greatest features of jQuery: Chainability. Notice this line of code: OK, so now the HTML within the "ChangeName.aspx" has been placed into the ' var options = {
method: 'POST',
url: 'ChangeName.aspx',
after: function(response) {
$("div#divView").html(response).show();
$("div#divEdit").empty().hide();
}
};
Notice that the above code also specifies the code to execute when a response is received from the form submission, in the " //bind to form's onsubmit event
$("form#ChangeName").ajaxForm(options);
Lastly, we'll hide the //hide the "edit" link
$("a#editName").hide();
//wire up the cancel link
$("a#lnkCancel").click(function() {
$("div#divView").show();
$("div#divEdit").empty().hide();
});
Now, let's take a look at the "ChangeName.aspx" page. Here's the HTML in its entirety (notice the absence of the <%@ Page language="c#" Codebehind="ChangeName.aspx.cs"
AutoEventWireup="false" Inherits="AJAXJQuerySample.ChangeName" %>
<form id="ChangeName" method="post" runat="server">
<asp:textbox id="txtName" runat="server"/>
<input type="submit" value="Save" id="Submit1" name="Submit1"/> or
<a href="#" id="lnkCancel">Cancel</a>
</form>
And, here is the code-behind file: protected System.Web.UI.WebControls.TextBox txtName;
private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack)
{
ProcessForm();
}
else
{
string existingName;
if (Session["testName"] == null)
{
Session["testName"] = "John Smith";
}
existingName = Session["testName"].ToString();
txtName.Text = existingName;
}
}
private void ProcessForm()
{
string newName = txtName.Text;
Session["testName"] = newName;
Response.Clear(); //clears the existing HTML
Response.ContentType = "text/plain"; //change content type
Response.Write(newName); //writes out the new name
Response.End(); //end
}
Note that we just need to use a regular if (Page.IsPostBack)
{
ProcessForm();
}
else...
The " string newName = txtName.Text;
Session["testName"] = newName;
Response.Clear(); //clears the existing HTML
Response.ContentType = "text/plain"; //change content type
Response.Write(newName); //writes out the new name
Response.End(); //end
That's it! Now, we can run our sample. Click the "Edit" link and the new form is loaded in. Change the name and click the "Save" button, and the updated name is sent back and displayed. Step 1: Click the "Edit" link:
Step 2: Change the name and click "Save":
Step 3: View the updated name:
ConclusionI have been using these techniques extensively, in my latest developments, to great success. I went this route because the new Microsoft AJAX tools were still in beta when I started, and I didn't want to hassle with the changes down the road. Again, I'm not claiming this to be a better solution than Microsoft AJAX (I've not done any comparisons), but if you're looking for another alternative, I would definitely suggest this technique. I've really enjoyed using jQuery, and have found that it makes it really easy to hook into the code wherever I want to add things like animations. Since switching to the jQuery JavaScript library, I now write about 10% the amount of JavaScript that I used to.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||