New Community Forums available at http://forums.ext.net
 

Preview of AjaxMethods

Thursday, November 27, 2008 posted by geoffrey.mcgill | 0 Comments

A huge new feature to be introduced with the next release (v0.7) of the Coolite Toolkit is the [AjaxMethod] Attribute.

Converting a standard server-side Method into an "AjaxMethod" enables the Method to be called directly from your client-side JavaScript. No PostBack, no page flicker and all communication is performed through lightweight AJAX + JSON.

The [AjaxMethod] Attribute can be applied to any public or public static .NET server-side Method (C#, VB, etc.).

An [AjaxMethod] is similar in functionaltiy to the ASP.NET [WebMethod] Attribute except an AjaxMethod is optimized to serialize and return data in an easily consumed JSON response.

Getting Started Basic Example


The following code sample demonstrates a simple AjaxMethod returning a string to the client.

Example
[AjaxMethod]
public string SayHello(string text)
{
return "Hello " + text;
}
Once the "SayHello" Method has been tagged with the [AjaxMethod] Attribute, the Method is now available to be called directly from JavaScript. The following sample demonstrates calling "SayHello" by clicking a hyperlink.

Example
<a href="#" onclick="Coolite.AjaxMethods.SayHello('World')">Click Me</a>
Once clicked, the SayHello JavaScript function will make an Ajax request back to the server and invoke the server-side SayHello Method. The string "Hello World" would be sent back to the browser packaged in a lightweight JSON object.

Example
{result:"Hello World"}
Calling SayHello returns a total response size of less than 25 characters... that's tight.

Success Handling


If the server-side AjaxMethod returns an object, we need to provide a way of handling that response. As the last parameter of the JavaScript function you can pass in an optional config object with a success handler.

Lets elaborate on our example by adding a <Click> Listener to an <ext:Button> Control and include the optional config object with a success function defined to alert the result.

Example
<ext:Button ID="Button1" runat="server" Text="Say Hello">
<Listeners>
<Click Handler="
Coolite.AjaxMethods.SayHello('World', {
success: doAlert
});" />
</Listeners>
</ext:Button>

<script type="text/javascript">
var doAlert = function (result) {
alert(result);
}
</script>

Details


So, lets break this down a bit. First off, the argument signature for the SayHello JavaScript function would be described as follows:

Example
SayHello(string text, object config)
The text argument is pretty simple, as it's the string sent into the server-side SayHello C# Method.

The config argument is a JavaScript object literal made up of a collection of name:value pairs. The JavaScript object literal is similar in concept to .NET Anonymous types. It's a "thing" (ie. object) with some properties.

In the example above, the success property is set with the name of a JavaScript function to call upon a successful response from the server.

Whatever object is returned from the server-side AjaxMethod is serialized into JSON, and passed as the return result into the success JavaScript function. Using our SayHello example, the result is passed to the doAlert JavaScript function.

The success function can also be coded inline within the config object.

Example
<ext:Button ID="Button1" runat="server" Text="Say Hello">
<Listeners>
<Click Handler="
Coolite.AjaxMethods.SayHello('World', {
success: function (result) {
alert(result);
}
});" />
</Listeners>
</ext:Button>

Summary


Adding the [AjaxMethod] Attribute to your server-side Method enables the Method to be called from client-side JavaScript running in a browser, without having to perform a PostBack. The request from the client to the web server is made using Ajax and the response from the server back to the client is packaged as a JSON object.

We're just scratching the surface here, so in future posts we'll dive deeper into performance tuning and advanced AjaxMethod configuration options.

More information and code samples demonstrating AjaxMethods are available in the recently updated Examples Explorer.

The Coolite Toolkit is a suite of powerful Ajax enabled ASP.NET Web Controls which simplify the development of Web Applications. The Coolite Toolkit includes the Ext JS JavaScript Framework and together are dual licensed with an open-source "Community" option or closed-source "Professional" version.

Labels: ,

ViewPort Preview

Thursday, April 10, 2008 posted by geoffrey.mcgill | 5 Comments

Hot on the heels of the BorderLayout Preview, is a preview for the new Coolite ViewPort ASP.NET web control.

When added to your Page, the ViewPort control will automatically size itself to the size of the browser window content area (viewport). The ViewPort control must contain a Layout control (typically BorderLayout). Within each Region of the BorderLayout, you can add other containers controls such as Panel, TabPanel, TreePanel or GridPanel.

ViewPort with BorderLayout


Child controls within a BorderLayout Region (North, South, East, West or Center) can themselves contain a Layout. By simply combining various Layout and Panel controls, a developer, within minutes can create elegant and functional application layouts which work consistently across all modern web browsers.

Code samples for both markup (<ext:ViewPort />) and C# code-behind are provided.

The sample also demonstrates the included "Gray" Theme, which can easily be set with one property at the Page, Session, Application or web.config levels.

The ViewPort control will be available with the version 0.5 release of the Coolite Toolkit.

Labels: ,

BorderLayout (and more) Preview

Wednesday, April 9, 2008 posted by geoffrey.mcgill | 3 Comments

The Layout controls for the version 0.5 release are coming together nicely. It's been a challenge, but I think we've found the sweet spot between clean markup, object model, Visual Studio designer support, Intellisense support and being true to the ExtJS framework.

The following examples provide code samples (markup + C#) and demonstrate the Layout controls within a Window control. The main Layout is controlled by a BorderLayout.

NOTE This build is not currently available for download. Everything you see will be included with the version 0.5 release.

Window with Simple Layout
Window with Complex Layout
All Layout controls, except FormLayout/FormPanel, are now working in both markup and code-behind. Design-time support for the Layout controls is still under development, but progressing quickly.

More v0.5 Notes and a few Breaking Changes

1. The ViewPort control will be included with the v0.5 release.

2. The TagPrefix has changed from 'cool' to 'ext'.

Example

// Old
<cool:window runat="server" id="Window1" />

// New
<ext:window runat="server" id="Window1" />

3. The Namespace (.dll) has changed to Coolite.Ext.Web from Coolite.Web.UI.

Example

// Old
Window win = new Coolite.Web.UI.Window();

// New
Window win = new Coolite.Ext.Web.Window();

4. The Grid (GridPanel) control *might* not be included with the 0.5 release. Completion of the Layout controls is further ahead than the GridPanel, so we might make the decision to release v0.5 early without including the GridPanel.

Any feedback is very much appreciated.

Labels: ,