New Features in ASP.NET AJAX 4
New Features in ASP.NET AJAX 4
Note The documentation in this section concerns ASP.NET 4 AJAX Preview 4. A more recent preview release might be available at the URL above. If so, it will include updated documentation.
The new functionality in ASP.NET AJAX enables new client data scenarios for page and component developers that enable JSON data from the server to be rendered as HTML in a highly manageable and efficient way. To enable these scenarios, ASP.NET AJAX includes the following major features:
· Client-side template rendering.
· Declarative instantiation of client-side behaviors and controls.
· Live data binding.
· Use of the observer pattern with JavaScript types.
· An AdoNetServiceProxy class for client-side interaction with ADO.NET Data Services.
· A client-side DataView control for data-bound UI in the browser.
· DataContext and AdoNetDataContext classes for interaction with Web services.
· Refactored ASP.NET AJAX framework libraries.
· Support for the DOM Ready event.
· Using JSONP to retrieve cross-domain data.
Client Template Rendering
In client-based development, templates are the most manageable way of creating UI from data. ASP.NET AJAX 4 includes a new template engine for client development that meets the following requirements:
· Performance — The engine must be able to render a typical number of items using a reasonably complex template before users perceive an interruption in their interaction with the application.
· Simplicity — The template syntax must be very readable and must be optimized for the most common scenario, namely one-way/one-time binding.
· Expression language — Templates must support an expression language to go beyond the simplest cases. The expression language should use familiar syntax, ideally JavaScript syntax.
· Interspersed code and markup — It must be possible to perform conditional rendering or to loop over markup by using code that surrounds HTML.
· Components — When using the template syntax, the developer must be able to instantiate client-side controls and behaviors that attach to HTML elements in the page or within templates.
Template Example
The following example shows a typical client template that you can create using ASP.NET AJAX 4.
<ul id="myTemplate" class="sys-template"
sys:attach="dataview"
>
<li>
<h3>{{ Name }}</h3>
<div>{{ Description }}</div>
</li>
</ul>
The class attribute of the outer div element is set to sys-template, which is a convention that is used in order to hide the initial template from the user. You should define this class in your CSS style sheet as {display:none;}.
When the template is rendered, it has a data item as context. Fields or properties of that data item can be included in the template markup by using {{ }} expressions — for example, {{ Name }}, if the data item has a Name field. These expressions can be placed anywhere in text content, or you can use them as the value of an attribute. In addition to fields or properties, the expression blocks can also contain any JavaScript expression that can be evaluated as a string.
You can set up DOM events by using the $addHandler method. The DOM on* attributes of elements (for example, onclick=method) also work from within templates.
Instantiating a Template by Using the DataView Control
The most convenient way to use client templates in ASP.NET AJAX 4 is through the DataView control. The content of a DataView control is used as a template that renders the data item that is provided to the control. If you set the DataView control's data property to an array, the template is rendered once for each item in the array. The DataView control binds to live data, meaning that the control is automatically updated when the data changes, without the need to rebind. This provides a dynamic data-driven UI in the browser. The following example shows the declarative markup for a DataView control that binds to an array named imagesArray.
<body xmlns:sys="javascript:Sys"
xmlns:dataview="javascript:Sys.UI.DataView"
sys:activate="*">
<ul sys:attach="dataview" class="sys-template"
dataview:data="{{ imagesArray }}"
>
<li>
<h3>{{ Name }}</h3>
<div>{{ Description }}</div>
</li>
</ul>
</body>
Instantiating a Template by Using Code
You can also create a compiled template from code by using the Sys.UI.Template class, as shown in the following example:
var t = new Sys.UI.Template($get("myTemplate"));
The constructor takes the parent element of the template as its argument. You can then render a compiled template into the DOM by calling its instantiateIn method and specifying an HTML container element and a data item as context. The following example shows how to do this.
t.instantiateIn(
$get("targetContainer"),
{
Name: "Name",
Description: "Description"
}
);
Using Pseudo-Columns in a Template
In addition to providing access to fields and properties of the data item, the template rendering engine also provides access to pre-defined "pseudo-columns" such as $index and $dataItem. These pseudo-columns give you access to values from the rendering engine at render time. You can use pseudo-columns the way you use any JavaScript variable in the template instance. The first example applies a different CSS class to the div element for alternating items that are rendered by the dataView control. The second examples passes the pseudo-column $dataItem, which represents the data for the current row, to a custom function named nameConvert for processing.
<div class:alternateitem="{{$index%2 == 1}}">
<span>{{nameConvert($dataItem)}}</span>
Incorporating Code into a Template by Using the code:if, code:before, and code:after Attributes
You can add the new code:if, code:before, and code:after declarative attributes to any DOM element within a template in order to render the element conditionally (code:if) or to render arbitrary code before (code:before) or after (code:after) the element. The following example shows how to use the code:If attribute to render an hr element as a separator between items. The code:if attribute uses the value of the $index pseudo-column to ensure that the hr element is rendered only between items, and not before the first one or after the last one.
<ul id="myTemplate" class="sys-template">
<li>
<hr code:if="$index!==0" />
<h3>{{ Name }}</h3>
<div>{{ Description }}</div>
</li>
</ul>
Posted in: asp.net | Tags: asp.net asp.net 4.0 performance feature asp.net ajax ajax dataview adonetdatacontext datacontext rendering simplicity template exampleNew Features in ASP.NET AJAX 4, part 4
The AdoNetServiceProxy Class
The AdoNetServiceProxy class enables read-write interaction with ADO.NET Data Services from JavaScript. The class enables access from JavaScript to a broad range of features of ADO.NET Data Services. It provides direct programmatic access to the basic REST operations provided by ADO.NET Data Services (insert, query, update, and remove), as well as many advanced features such as verb tunneling (custom HTTP methods) and optimistic concurrency.
The AdoNetServiceProxy class is used by the AdoNetDataContext class in read-write scenarios, as explained in the next section.
The DataContext and AdoNetDataContext Classes
For read-write scenarios that use Web services or data services, ASP.NET AJAX 4 provides a DataContext class that provides full support for change tracking in the browser. This enables complete end-to-end AJAX-based data scenarios.
Typically, data is fetched from the server through JSON services such as a WCF AJAX-enabled service or ADO.NET data services. The data is displayed to the user through dynamic data-driven UI, using the DataView control. Declarative live-binding markup in the template provides users with an edit UI, which enables them to modify the data. The ASP.NET AJAX DataContext class tracks all changes to the data automatically. All changes can then be sent at once to the server by calling the SaveChanges method of the DataContext class. A single DataContext instance can manage change tracking for data returned by different operations on the server, even if the operations return different types of objects.
The following example shows how to use the DataContext class.
<script type="text/javascript">
var dataContext = new Sys.Data.DataContext();
dataContext.set_serviceUri("../Services/imagesService.svc");
dataContext.set_saveOperation("SaveImages");
dataContext.initialize();
</script>
<button onclick="dataContext.saveChanges()" class="right">Save Changes</button>
<ul sys:attach="dataview" class="sys-template"
dataview:dataprovider="{{ dataContext }}"
dataview:query="GetImages"
>
<li>
<input type="text" value="{binding Name}"/><br/>
<input type="text" value="{binding Description}"/>
</li>
</ul>
If you are using an ADO.NET data service, you should use the AdoNetDataContext class instead of the more general-purpose DataContext class. (AdoNetDataContext derives from DataContext.) The AdoNetDataContext class provides additional support for features that are specific to ADO.NET, such as identity management, links and associations between entity sets that are returned in different fetch operations, hierarchical data, and optimistic concurrency.
Refactoring the Microsoft AJAX Framework Libraries
ASP.NET AJAX 4 also introduces the ability to use only parts of the ASP.NET AJAX framework for efficiency, as well as the ability to use the ScriptManager control without using the ASP.NET AJAX framework at all. The ScriptManager control provides services such as centralized management of references, support for debug and release modes, support for localization, and script combining. These services can be useful to all client-script developers, even those who use JavaScript libraries other than Microsoft AJAX, such as jQuery. Until now, the ScriptManager control included the Microsoft ASP.NET AJAX library automatically, without providing a simple way to opt out of it. As a result, when developers used the ScriptManager control in their pages, the effect was to include the whole Microsoft ASP.NET AJAX library in their applications.
In ASP.NET 4, the default behavior for the ScriptManager control is to include the complete ASP.NET AJAX library. However, the ScriptManager control supports a new MicrosoftAjaxMode property that lets you choose a subset of the framework by using only portions of the library in the form of split script files. The MicrosoftAjaxMode property can have one of the following values:
· Enabled — All Microsoft AJAX scripts are included (legacy behavior). This is the default value of the property.
· Explicit — Each split script file must be added explicitly; it is up to you to make sure that you include all scripts that have dependencies on one another.
· Disabled — All Microsoft ASP.NET AJAX script features are disabled and the ScriptManager control does not reference any scripts automatically.
When you use Explicit mode, the scripts that are available are as follows:
· MicrosoftAjaxCore.js
· MicrosoftAjaxComponentModel.js
· MicrosoftAjaxSerialization.js
· MicrosoftAjaxGlobalization.js
· MicrosoftAjaxHistory.js
· MicrosoftAjaxNetwork.js
· MicrosoftAjaxWebServices.js
· MicrosoftAjaxApplicationServices.js
· MicrosoftAjaxTemplates.js (New for ASP.NET AJAX 4)
· MicrosoftAjaxAdoNet.js (New for ASP.NET AJAX 4)
The following chart shows the dependencies between split scripts. The violet boxes that are labeled Templates (AdoNetDataContext) and Templates (DataContext) indicate that only these classes in MicrosoftAjaxTemplates.js have the indicated dependency. Thus, MicrosoftAjaxTemplates.js does not require MicrosoftAjaxWebServices.js unless you use the DataContext class, and it does not require MicrosoftAjaxAdoNet.js unless you use the AdoNetDataContext class.
The WebForms box for the MicrosoftAjaxWebForms.js script is green to indicate that it is included automatically when you set EnablePartialRendering to true (which is the default setting for the ScriptManager control).
As an example, to use the ASP.NET AJAX browser history feature with no partial rendering and with MicrosoftAjaxMode set to Explicit, the ScriptManager control must be configured as in the following example:
<asp:ScriptManager ID="ScriptManager1"
EnablePartialRendering="false"
MicrosoftAjaxMode="Explicit"
EnableHistory="true"
runat="server">
<CompositeScript>
<Scripts>
<asp:ScriptReference Name="MicrosoftAjaxCore.js" />
<asp:ScriptReference Name="MicrosoftAjaxComponentModel.js" />
<asp:ScriptReference Name="MicrosoftAjaxSerialization.js" />
<asp:ScriptReference Name="MicrosoftAjaxHistory.js" />
</Scripts>
</CompositeScript>
</asp:ScriptManager>
In the example, EnablePartialRendering has been set to false so that MicrosoftAjaxWebForms.js is not included automatically, and EnableHistory has been set to true so that the ScriptManager control can use the AJAX browser history feature. Order is important when you include split scripts — if a script has dependencies, the ScriptReference elements for those dependencies must be listed before the ScriptReference element for the script itself.
Note that split script files should be used only by developers who are concerned about optimizing for very high performance. When split script files are used, they should be used together with script combining to minimize the numbers of requests that are required in order to download the scripts.
The DOM Ready Event
In Beta 2, changes in ASP.NET make it easier to use Microsoft AJAX when you create an ASP.NET MVC application or pure client Web application. In versions earlier than Beta 2, if you wanted to use the pageLoad method outside a Web Forms application, for performance reasons you needed to call sys.application.initialize at the bottom of the page. Otherwise, the pageLoad method would not be called until the window.onload event was raised.
In Beta 2, the pageLoad method is called immediately after the document DOM content is finished loading and before the window.onload event. You no longer need to call sys.application.initialize to cause the pageLoad method to be called before window.onload occurs.
Using JSONP to Retrieve Cross-Domain Data
Ordinarily, when you make an AJAX request from a page, you are limited to making a request to the domain that hosts the page. In other words, you are not allowed to make cross-domain requests.
In Beta 2, ASP.NET AJAX includes support for JSONP. Using JSONP, you can interact with services that are located in another domain.
No special configuration is required in order to take advantage of JSONP when you create a service reference. You simply configure the service path of the service to include a reference to the target domain, as shown in the following example:
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="http://AnotherDomain/Books.svc" />
</Services>
</asp:ScriptManager>
In order to use JSONP to make cross-domain requests, you must interact with a JSONP-enabled service. For example, if you want to request data from a WCF service, the service must be enabled to support JSONP. For more information about how to enable JSONP for a WCF service, see the article JSON with Padding (AJAX) on the MSDN Web site.
Posted in: | Tags: asp.net asp.net 4.0 asp.net ajax ajax adonetdatacontext datacontext adonetserviceproxy ajax framework json jquery microsoftajaxglobalization microsoftajaxhistory microsoftajaxapplicationservices microsoftajaxadonetASP.NET AJAX 4.0 Preview 5
ASP.NET AJAX is a pure client-side JavaScript library that enables you to build Ajax applications that work with all modern web browsers. By taking advantage of ASP.NET AJAX client controls and templates, you can build database-driven web applications that execute entirely in the browser.
ASP.NET AJAX supports the following features:
- Client templates that can be used to format database data in the browser
- Client controls such as the DataView control
- Client DataContext component that supports change tracking and identity management
- ADO.NET Data Services support
- WCF and ASMX Web service support
- JSONP support
- Observable pattern for plain JavaScript objects
- Live Bindings
- Command Bubbling
- Support for managing complex links and associations between entities from multiple entity sets or tables
In Preview 5 of ASP.NET AJAX 4.0 we continue to improve the client-side data story introduced in previous previews of ASP.NET AJAX. In this release, we add support for the following features:
- Dynamic and recursive templates
- Binding Converters
- Compatibility with the ASP.NET UpdatePanel
download: http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=32770
Posted in: asp.net | Tags: wcf asp.net ajax dataview datacontext json ajax 4.0 ajax 4.0 preview preview 5 asmx updatepanel