How to: Insert Data with 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 on the same Web site as the current page.
The example in this procedure shows an AJAX-enabled Web page that enables the user to insert a new record in the Customer table of the Northwind database.
To run the example code in this topic, you will need the following:
Inserting Data through a Data Service
You can create a page that uses client script to insert new data through a data service.
To insert data through the Data Service
-
Create a script element or JScript file that includes a client-script function that does the following:
-
Creates an instance of the DataService class.
-
Creates the JavaScript object that contains the data to insert.
-
Calls the insert method, and passes as parameters the object with the data and the relative URI of the resource that will receive the data. If needed, pass references to callback functions for success and error conditions. You can also pass an optional parameter containing a context or state object. This parameter is passed to the succeeded or failed callback function when the request returns.
The following example shows a function named doInsert that performs these tasks. The DataService instance references a fictitious data service that has the relative URI /Northwind.svc. The newCustomer variable includes a JavaScript object that contains customer information that is derived from the values of text boxes on a Web page. The call to the insert method references cbSuccess as the succeeded callback function, and cbFailure as the failed callback function.
function doInsert() {
var northwindService =
new Sys.Data.DataService("/Northwind.svc");
var newCustomer = {
CustomerID: $get("TextCustomerId").value,
CompanyName: $get("TextCompanyName").value,
ContactName: $get("TextContactName").value
};
northwindService.insert(newCustomer, "/Customers", cbSuccess, cbFailure);
}
-
If needed, add callback functions for succeeded and failed operations. The callback functions take up to three parameters, one for the result or error, an optional context, and an optional operation name.
The following example shows a succeeded and a failed callback function.
function cbSuccess(result, context, operation) {
$get("spanResults").innerHTML =
"Operation '" + operation + "' succeeded.";
}
function cbFailure(error, context, operation) {
$get("spanResults").innerHTML =
"Operation '" + operation + "' failed.";
}
-
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/InsertExample.js" />
-
Add any required user interface elements to the Web page.
The following example shows a Web page that includes text boxes that are referenced by the JavaScript code that you created in step 1.
Use this form to insert a new Customer into the Northwind database.<br />
<table style="width:100%;">
<tr>
<td class="style1">
Customer ID:</td>
<td>
<input id="TextCustomerId" type="text" />
<span class="style2">(five characters, e.g. 'SAMPL')
</span>
</td>
</tr>
<tr>
<td class="style1">
Company Name:</td>
<td>
<input id="TextCompanyName" type="text" /></td>
</tr>
<tr>
<td class="style1">
Contact Name:</td>
<td>
<input id="TextContactName" type="text" /></td>
</tr>
</table>
<br />
<span id="spanResults"></span>
-
Add code or an HTML element that calls the JavaScript function that performs the insert operation.
<input id="ButtonInsert" type="button" value="Create"
onclick="doInsert()" /><br />
<br />
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.