How to: Execute a Sequence of Actions Through a Data Service Using AJAX
Introduction
You can access an ADO.NET Data Service from an AJAX application by using the Sys.Data.DataService class from the ASP.NET Extensions release. From client script, you can query and modify data that is exposed by a data service, as long as the data is on the same Web site as the current page. In some cases, you need to execute more than one data operation as a single batch. If so, you can use the Sys.Data.ActionSequence script class to create a batch of data operations that are executed in the order they were added, rather than executing each operation as a separate command.
The example in this procedure shows an AJAX-enabled Web page that lets the user insert ten records in the Customers table of the Northwind database.
To run the example code in this topic, you will need the following:
-
The ASP.NET AJAX Client Library for ADO.NET Data Services. You can download the latest release from the ASP.NET AJAX section of the Codeplex Web site.
-
The Northwind database installed on your computer.
-
An ADO.NET Data Service that exposes the Northwind database.
-
An ASP.NET Web site that is created with the ASP.NET Extensions.
-
An ASP.NET Web page where you can add code to invoke a Web service.
Executing a Sequence of Actions
You can create a page that uses client script to execute multiple data operations through a data service.
To execute a sequence of actions
-
Create a script element or a JScript file that includes a client-script function that does the following:
-
Creates an instance of the DataService class.
-
Calls the createActionSequence method to create an instance of the ActionSequence JavaScript object.
-
Calls the addInsertAction method, addUpdateAction method, or addRemoveAction method as many times as needed in order to create a sequence of data operations. In each call, you pass as parameters the object with the data, and the relative URI of the resource that will receive the data. You can also pass a parameter with context or state, which is passed to the callback function when the request returns.
-
Calls the executeActions method to execute all the data operations that have been added to the sequence. If needed, you can pass a callback function that is called when the data operations have been executed.
The following example shows a function named pageLoad that creates an instance of the DataService class. The instance references a fictitious data service that has the relative URI /Northwind.svc. The function named ExecuteOperations calls the createActionSequence method to create an instance of the ActionSequence script class. The function includes code that adds ten insert operations to the sequence. Finally, it calls the executeActions method and passes cbComplete as the callback function.
var northwindService;
function pageLoad() {
northwindService = new Sys.Data.DataService("/Northwind.svc");
}
function ExecuteOperations() {
var actSequence = northwindService.createActionSequence();
// Create 10 customer objects and insert them.
for (var i = 0; i < 10; i++) {
var customer = {
CustomerID: "TEST" + i,
CompanyName: "Sample Company " + i
};
actSequence.addInsertAction(customer, "/Customers");
}
actSequence.executeActions(cbComplete, "insert 10 Customers" /* user-provided context */);
}
-
If needed, add a callback function for the completed sequence of operations.
Each callback function takes up to three parameters:
-
An array of ActionResult objects that represent the results of each operation.
-
A Boolean value that indicates whether an error has occurred during any part of the operations.
-
A state object for the batch of operations.
The following example shows a succeeded callback function.
function cbComplete(result, hasError, context) {
alert("Operation '" + context + "' " + ((!hasError) ? "succeeded." : "experienced at least one failure."));
}
-
On the Web page that you are using to invoke the data service, add a ScriptManager control with a Scripts collection and a ScriptReference element that references MicrosoftAjaxDataService.js, as shown in the following example.
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Scripts>
<asp:ScriptReference Name="MicrosoftAjaxDataService.js" />
</Scripts>
</asp:ScriptManager>
-
If you created the JavaScript function in step 1 as a JScript file, add a reference to that file in the ScriptManager control. To register a static script file, set the Path property of the ScriptReference object to the relative location of the file, as shown in the following example:
<asp:ScriptReference Path="scripts/SequenceExample.js" />
-
Add code or an HTML element that calls the JavaScript function that performs the insert operation.
<input id="btnInsert"
type="button"
value="Add 10 New Customers"
onclick="return ExecuteOperations()"
/>
This topic is ASP.NET Extensions documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.