Walkthrough: Creating an AJAX-enabled Web Site

This walkthrough creates a basic ASP.NET Web site with a Web page that illustrates some features of the ASP.NET AJAX Library that is included with Visual Studio. You will build an application that displays pages of employee data from the AdventureWorks sample database. The application uses the UpdatePanel control to refresh only the part of the page that has changed, without the page flash that occurs with a postback. This is referred to as a partial-page update. The sample application also uses the UpdateProgress control to display a status message while the partial-page update is processing.

Prerequisites

To implement the procedures in your own development environment you need:

  • Microsoft Visual Studio 2005 or Microsoft Visual Web Developer Express Edition.

  • The AdventureWorks sample database. You can download and install the AdventureWorks database from the Microsoft Download Center. (Search for "SQL Server 2005 Samples and Sample Databases (December 2006)").

Creating a Web Site

If you have already created a Web site in Visual Web Developer (for example, by following the steps in Walkthrough: Creating a Basic Web Page in Visual Web Developer), you can use that Web site and skip to the next section, Creating the Master Page. Otherwise, create a new Web site and page by following these steps.

To create a file system Web site

  1. Open Visual Web Developer.

  2. On the File menu, click New Web Site.

    The New Web Site dialog box appears.

  3. Under Visual Studio installed templates, click ASP.NET Web Site.

  4. In the Location box, enter the name of the folder where you want to keep the pages of your Web site.

    For example, type the folder name C:\WebSites.

  5. In the Language list, click the programming language you prefer to work in.

  6. Click OK.

    Visual Web Developer creates the folder and a new page named Default.aspx.

Adding an UpdatePanel Control to an ASP.NET Web Page

After you create a Web site, you create an ASP.NET Web page that includes an UpdatePanel control. Before you add an UpdatePanel control to the page, you must add a ScriptManager control. The UpdatePanel control relies on the ScriptManager control to manage partial-page updates.

To create a new ASP.NET Web page

  1. In Solution Explorer, right-click the name of the site and then click Add New Item.

    The Add New Item dialog box is displayed.

  2. Under Visual Studio installed templates, select Web Form.

  3. Name the new page Employees.aspx and clear the Place code in separate file check box.

  4. Select the language you want to use.

  5. Click Add.

  6. Switch to Design view.

  7. In the AJAX Extensions tab of the toolbox, double-click the ScriptManager control to add it to the page.

    UpdatePanel Tutorial
  8. Drag an UpdatePanel control from the toolbox and drop it beneath the ScriptManager control.

    UpdatePanel Tutorial
Adding Content to an UpdatePanel Control

The UpdatePanel control performs partial-page updates and identifies content that is updated independently of the rest of the page. In this part of the walkthrough, you will add a data-bound control that displays data from the AdventureWorks database.

To add content to an UpdatePanel control

  1. From the Data tab of the toolbox, drag a GridView control into the editable area of the UpdatePanel control.

  2. In the GridView Tasks menu, click Auto Format.

  3. In the Auto Format panel, under Select a scheme, select Colorful and then click OK.

  4. In the GridView Tasks menu, select <New data source> from the Choose Data Source list.

    The Data Source Configuration wizard is displayed.

  5. Under Where will the application get data from, select Database and then click OK.

  6. In the Configure Data Source wizard, for the Choose Your Data Connection step, configure a connection to the AdventureWorks database and then click Next.

  7. For the Configure the Select Statement step, select Specify a custom SQL statement or stored procedure and then click Next.

  8. In the SELECT tab of the Define Custom Statement or Stored Procedures step, enter the following SQL statement:

    SELECT FirstName, LastName FROM HumanResources.vEmployee ORDER BY LastName, FirstName
    
  9. Click Next.

  10. Click Finish.

  11. In the GridView Tasks menu, select the Enable paging check box.

  12. Save your changes, and then press CTRL+F5 to view the page in a browser.

    Notice that there is no page flash when you select different pages of data. This is because the page is not performing a postback and updating the whole page every time.

Adding an UpdateProgress Control to the Page

The UpdateProgress control displays a status message while new content for an UpdatePanel control is being requested.

To add an UpdateProgress control to the page

  1. From the AJAX Extensions tab of the toolbox, drag an UpdateProgress control onto the page and drop it beneath the UpdatePanel control.

  2. Select the UpdateProgress control, and in the Properties window, set the AssociatedUpdatePanelID property to UpdatePanel1.

    This associates the UpdateProgress control with the UpdatePanel control that you added previously.

  3. In the editable area of the UpdateProgress control, type Getting Employees ... .

  4. Save your changes, and then press CTRL+F5 to view the page in a browser.

    If there is a delay while the page runs the SQL query and returns the data, the UpdateProgress control displays the message that you entered into the UpdateProgress control.

Adding a Delay to the Sample Application

If your application updates each page of data quickly, you might not see the content of the UpdateProgress control on the page. The UpdateProgress control supports a DisplayAfter property that enables you to set a delay before the control is displayed. This prevents the control from flashing in the browser if the update occurs very fast. By default, the delay is set to 500 milliseconds (.5 second), meaning that the UpdateProgress control will not be displayed if the update takes less than half a second.

In a development environment, you can add an artificial delay to your application to make sure that the UpdateProgress control is functioning as intended. This is an optional step and is only for testing your application.

To add a delay to the sample application

  1. Inside the UpdatePanel control, select the GridView control.

  2. In the Properties window, click the Events button.

  3. Double-click the PageIndexChanged event to create an event handler.

  4. Add the following code to the PageIndexChanged event handler to artificially create a three-second delay:

    Visual Basic
    'Include three second delay for example only.
    System.Threading.Thread.Sleep(3000)
    
    C#
    //Include three second delay for example only.
    System.Threading.Thread.Sleep(3000);
    
    Bb386580.alert_note(en-us,VS.90).gifNote:

    The handler for the PageIndexChanged event intentionally introduces a delay for this walkthrough. In practice, you would not introduce a delay. Instead, the delay would be the result of server traffic or of server code that takes a long time to process, such as a long-running database query.

  5. Save your changes, and then press CTRL+F5 to view the page in a browser.

    Because there is now a three-second delay every time that you move to a new page of data, you will be able to see the UpdateProgress control.

See Also

Tasks

Concepts

Tags :


Community Content

Thomas Lee
Needs updating for Visual Studio 2008

Q) In VS 2008 there is no web site template called: "ASP.NET AJAX-Enabled Web Site".

A1) Apparently it's built in to the normal template already. So you just need to add a New Item using the template AJAX Web Form.

A2) Above answer is very dubious, if you use VS 2008 (.NET 2.0 framework) and regular ASP.NET Web Application, you will be greeted with the nice ' "Sys" is undefined ' error message. So there is something need to be done here, people.


Doron A
Table change in SQL
The walkthrough worked well in VS 2008 with one exception: in my copy of the Sql Server 2005 Adventureworks database, there isn't a HumanResources.vEmployee table. Instead there is a HumanResources.Employee table but that doesn't work either because there are no LastName or FirstName fields in that table. These are stored in the Person.Contact table. With that table substituted for the specified one, everthing works. I used a standard web form as instructed. An AJAX web form is created with a scriptmanager control so the step where that is added to the form could be eliminated by choosing the Ajax web form.

The walkthrough worked great in VS 2008, with no exceptions. It is true that there is no HumanResources.vEmployee table, because this object (vEmployee) is not a table but rather a view.
Tags : contentbug

Thomas Lee
Combined with PostBackUrl
The walkthrough worked for me, but when I try to incorporate the UpdateProgress in a page that has a master page and a PostBackURL to a different page, it doesn't work. In this scenario, the search page posts to a results page. I think it's because of the posting to a different page (the life cycle of the page ends on the post).
Otherwise, this is a nice feature!
Tags : contentbug

tamilprabhu
Creating an AJAX Enabled website in Aspnet 2.0
How to create the Ajax included webpage in .net2008 . am trying to open but only default web site are available there no other option for Ajax included webpage
Tags :

ensee
Visual Studio 2008 installs
I had to install ASPAJAXExtSetup.msi and ASPNETAJAXVS2008.msi on my 2008 install, then I was able to complete the walkthrough successfully.
Tags :

jsk5239
Visual Studio 2008 installs

Thanks ensee! This worked.

Tags :

Page view tracker