asp.net AJAX 4.0 Preview 3 (Part 1

Source: Internet
Author: User

Since Microsoft worked with jquery, asp.net ajax and jquery have been positioned as two complementary Ajax libraries. Now that jquery has implemented so many lightweight Ajax features, natural asp.net Ajax will continue to focus on some of the heavyweight features required for rich clients.

In asp.net Ajax 4.0 Preview 3, two important new features that developers have access to are the Ado.net Data Service Client Library and asp.net ajax Template. For people who are familiar with ASP.net server-side development but are unfamiliar with client development, you can simply interpret these two features as DataSource and ListView that exist on the client, as long as the data is exported to the front end through the ado.net You can develop data-driven applications on the client as you would with the combination of DataSource and ListView.

In this article, we will look at how to use the Ado.net Data Service client Library to call the rest data interface exposed by the Ado.net data Service directly to the client JavaScript code. The example code used in the article is at the end of the article, and then refer to the adonetdataservicedemo.aspx inside.

Server-Side Preparation

In our next example, we'll use a SQL Server, Express Edition database with a table named Oscarwinners that records the Oscars for this year's Oscar-winning list, with fields including Awardid, award, Winner, Film. Then we create the Ado.net Entity Model for this table, and then create the Ado.net Data Service for the entity classes it generates. These are the operations that can be done with a few mouse clicks in Visual Studio 2008, and are no longer explained in detail. Within the InitializeService () method of the Ado.net Data service, we provide only one of the most lenient rules:

config.SetEntitySetAccessRule("*", EntitySetRights.All);

Here we have the server-side work ready. Open the Ado.net Data service address you created to see if the correct atom format data is being exported. If not, check to see if WCF is properly installed and configured on your machine. Make sure that the server-side preparation is done and then enter the client's development work.

Connecting to the data Service

In the client use Ado.net Data service, we need to contact the class only one, that is Sys.Data.AdoNetServiceProxy. First, we're going to connect to the Ado.net data service, which is to instantiate this class using the URL of the Ado.net data service:

var dataService = new Sys.Data.AdoNetServiceProxy("WebDataService.svc");

We can then use DataService to invoke the Ado.net Data service for CRUD operations.

CRUD Operations

All CRUD operations are performed on the Sys.Data.AdoNetServiceProxy object, named Query (), insert (), update (), remove (), respectively. In our example, the query () and update () methods are used, and the other two methods are similar to the update () and are not explained in detail.

Query operations

dataService.query("OscarWinners", function(result, context, operation) {
  /* display result */
}, errorHandler);

Using the above statement, we query all the data in the Oscarwinners table. The first callback function that follows will be invoked when the query succeeds, so we can write the logic of stitching HTML to display the result, and refer to the adonetdataservicedemo.aspx in the download for the specific code. The second callback function is invoked when the query fails, and we can write a unified error-handling function called ErrorHandler and pass it to this parameter.

If you need to pass complex query parameters, use the format of the Ado.net Data service, which can be found on MSDN. For example, query Slumdog Millionaire how many Oscars the film won, and then sorted the awards by name output, you can write this:

dataService.query("OscarWinners?$filter=Film eq 'Slumdog Millionaire'&$orderby=Award", function(result, context, operation) {
  /* display result */
}, errorHandler);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.