Risk of using AjaxControlToolkit

I like the AjaxControlToolkit, but using those controls without a real knowledge of what is happening behind the scenes can be dangerous. We have a simple page that shows a list of objects of type A; for each line there is a button called “Associate”, that opens a modal-like dialog where the user can search objects of type B. Each result of this search show a button call “Select” to associate that object to the A object. The page was written with ModalPopupExtender; for each line a User Control (that implements search and association) was included and managed by a ModalPopupExtender, a few lines of code behind, an updatePanel and the game is done, whoa, success

The page was cool, thanks to updatepanel and partial rendering the user does not see page to flicker and thanks to modalPopupExtender all the logic is reduced to very few lines of code. This experience makes programmers loving those kind of controls, until the number of users grow and people start to complain about slow performance. Let’s examine only the bandwidth consumed by this page and do not check other possible performance issue.

RequestBytes uploadedBytes downloaded
Open the page074,918
Press Associate button00
Search Objects136,275196,682
Press Select to associate an object136,323196,770
Press Close Button to close the modal136,171446,582

WHAT!!!!!! these are pretty big numbers for such a simple operation (search and asosciate), but here is what is happening. First of all we use a custom HttpModule that compress the page before sending back to the caller, but it creates problems with the UpdatePanel, so each request made by an update panel (partial rendering) does not get compressed, and this is the reason why the page size is much bigger on subsequent requests. Then we have a big upload payload due to the huge viewstate of the page (there are a lot of other stuff in the page) and each operation issue a full POST roundrip to the server…. a lot of traffic

The solution is using a real Ajax technique, I created a separate page to render the grid, and with a little bit of jQuery moved all this logic into a true Partial Rendering Ajax page. The page that manage the association only accepts GET request with all needed parameters and only render the grid. Now I did the same sequence of operation and look at the different result.

RequestBytes uploaded Bytes downloaded
Open the page048,414
Press Associate button01,744
Search Objects01,662
Press Select to associate an object1,658247
Press Close Button to close the modal00


The original request is much smaller because I removed all those Panels that were handled with ModalPopupExtender, so the whole page is smaller.

When the user press the Associate Button the page issue a GET request to the other page that renders a small bunch of HTML (a texbox to search and a button inside a div), and when the user press the Search Button I returned only resulting data formatted in a simple Table. Finally when the user press Select I call an asmx webservice thanks to Ajax.Net to create the association in the database, and finally pressing the close button simply removes all partial rendering content with javascript, so no roundtrip is needed.

This big improvement in performance was paid by more time needed to develop the solution, by the gain in performance and scalability is tremendous, and since this is one of the most common operation the user to with this web application, the overall improvement worth the time spent on it and the slightly more complicated code on the UI.

This kind of problems happen because using the Update Panel or other similar control (like the ModalPopupExtender) gave the programmer the sense of creating a real Ajax application, quite often I heard people telling me that the concept of ajax is only “avoid refreshing the whole page” so an update panel is the only control they need.

Such kind of problems arise because developers are used to test application locally, where size of the page does not impact on perceived performance. I strongly suggest you to constantly test your web application with tools capable of simulating various Bandwidth scenario, just to avoid to find problems only when you deploy the application on the production server just to verify that your server bandwidth is completely saturated by the app.


jQuery to the rescue – some numbers

In an old project I have a particular user control dedicated to identify data stored in a tree. The user can type some letters, and press “Search” button to find all the elements that match the search, or it can use a series of DropDownList to navigate from the higher element down to the hierarchy. It is used extensively in the site, and is used even to select location, from a table where location are stored in Nations, Region, Province and Location.

The control was designed as a user control, wrapped in an update panel, it works well, but it is really inefficient for high number of operations, so we decided to make a different version in jQuery. It tooks me about one day to recreate the control, mainly to maintain all the functionality it has in the past. Since the older version have already a decoupled DAL, I can reuse all server functions to search, manage the tree etc, and I need to write less code.

The old code was spread in four user control whose length is: 356, 557, 367 and 310 lines of code. This is mainly due to the fact that I decided to create two controls, one for the free text search, and the other for the DropDownList navigation, and other two controls that wire everything up. This kind of structure was chosen, because in some page we want to display only the DDL, in other only the search, and having each functionality in its own user control was simple to manage, I have only to change visibility.

Now we are testing the new control, it seems to work excepts some minor bugs that needs to be corrected, but I’m able to give you some numbers.

The control now is contained in only two server files and one javascript file. The first server control is the one used to render HTML and is 293 lines length (it is composed only by properties and some little logic, so it has really logic to mantain), the other file is an aspx page that stream json result data to the caller, and is 141 lines long, the js file is 192 lines and it is composed mainly by comments that explain how the script work. I’m happy because I reduced a lot server code, and the overall structure is simplier. Now it is time to look at request and response length. I recorded with fiddler a little section in witch I first search and select an option, then I use DropDownList part to select in a four level tree. I recorded both old version and new one in a real complex page of the program, here is the result.

First search3684013320215432 
First combo8342000Already loaded into the page
second combo66101543535721 
third combo15801565236228 
fourth combo538701580437672 

I immediately notice is that the old version does a lot of traffic. Since the new version only does GET reques it reduces the upload payload from 178k to zero !!!!! The download is also reduced and is about 6% of the original size. These numbers makes me think a lot, because Asp.Net is a great environment that can be used to do RAD developement, but sometimes it is not so efficient. Thanks to jQuery we can still use asp.net optimizing some critical part with client logic.



Asp.net Mvc plus jQuery client template engine .. have fun with them

jQuery has really a lot of interesting plugins, but the one I like most is a template engine called jtemplates. Basically it consist of a jQuery extension that is capable of rendering html with javascript on client machine. You really have a lot of flexibility on how to specify a template, basically you can embed it into an hidden textarea, the one you see in the above sample is created in this way

    <textarea id="template" style="display:none">
        <div>{$T.name}: {$T.list_id} [{$P.lang.language}]</div>
            <thead style="font-weight: bold">
                {#foreach $T.table as record}

As you can see jtemplates has a simple syntax to render data where $T identify the object that contains data to be rendered. The template engine supports many constructs: foreach, if, and many other ones. To actually render something you need to assign the above template to a div:


Assigning a template is just a matter of selecting a wrapped-set and then assign the template passing the id of the textarea that contains the template. Now the div has a template assigned, to render something you need only to fire the template engine passing it the object that contains data to be rendered. It is accomplished with the function processTemplate(data). The data is a simple javascript object that will be substituted to the $T object inside the template.

This is not the only way to set a template, the other one is using setTemplateURL and processTemplateURL that actually gets the template from an URL and render it with a json object returned from the url passed to processTemplateURL. If this sounds you interesting, think to asp.net mvc, where you can create a controller that manages templates, and other controllers that returns data with JsonResult. If you structure the site in this way, you can maximize performance passing only json data with the server. You only need to render the page with empty divs, assign template to the div, and finally gets data to be rendered from the server.

Actually I’m experimenting with the PhotoAlbum application I did for the jQuery workshop of our usergroup DotNetMarche, as soon as possible I’ll begin to post some concrete code that works with client-side rendering, showing you pratical example that uses this technique.



Create a list of attachments with jQuery and ajax calls.

I’ve found this exceptional component to do file upload with jQuery and ajax call. I have a page where I need to edit a domain entity that have a property of type List<FileAttach>, and I need to permit to the user to easily add and remove attachments. The asp.net webform where this control resides is quite complex, so I really want to avoid a full page postback each time the user want to Add, remove or download an attachment.

My technique is quite simple, I created a page called AttachmentAjaxAction.aspx, this pages have two main functions, the first is to render a grid with all the attachments of related entity, the other is to add or remove an attach from the list. It accepts some parameter in post, and it does all the work in the load event.

      Dim command As String = Request.Form("command")
      Dim entityid As Guid = New Guid(Request.Form("relid"))
      Dim service As New InterventionService

      If command = "add" Then
         Dim attach As New FileAttach()
         attach.FileName = Request.Files(0).FileName
         Dim buffer(8192) As Byte
         Dim read As Integer
         read = Request.Files(0).InputStream.Read(buffer, 0, buffer.Length)
         Using ms As New MemoryStream
            While read > 0
               ms.Write(buffer, 0, read)
               read = Request.Files(0).InputStream.Read(buffer, 0, buffer.Length)
            End While
            attach.FullContent = New FileAttachData()
            attach.FullContent.Data = ms.ToArray()
         End Using
         service.AddAttachmentToIntervention(entityid, attach)
      ElseIf command = "del" Then
         Dim attachid As Guid = New Guid(Request.Form("attachid"))
         service.DeleteAttach(entityid, attachid)
      End If
      Dim interventino As DomainIntervention = service.GetInterventions(domaininterventionid)
      grdIntervention.DataSource = interventino.Attachments

The code is really simple, all parameters are passed in post variables, when the action is “add” I need to check the request.Files collection because this indicates that a file was uploaded to the server, when the action is “del” I simply delegate to the service the task to delete the attach. Finally after all operations are completed the page renders the whole grid. Files are stored in its binary form directly in the database (to use full text search). Now in the main page, I simply place a div where I want this grid to be shown.

 <asp:Panel ID="pnlAttachments" runat="server">

     <div id="attachmentgrid" relid='<%# Eval("Id") %>' style="min-height:100px;min-width:300px;"><h2>Attendere caricamento lista attach in corso</h2>
      <input id="btnUpload" relid='<%# Eval("Id") %>' type="button" value="Aggiungi" />

I simply create an header, a div and an html button both with the attribute relid equal to the id of the entity we are managing. Now it is time for jQuery to wire everything with a simple script. In the first part I wireup the plugin used to upload the file

$(document).ready(function() {
   if ($('#btnUpload').size() == 1) {
    new AjaxUpload('#btnUpload', {
      action: 'AttachAjaxAction.aspx',
      name: 'uploadedfile',
      data: {
         relid: $('#btnUpload').attr('relid'),
         command: 'add',
      autoSubmit: true,
      responseType: false,
      onChange: function(file, extension) { },
      onSubmit: function(file, extension) { 
      onComplete: function(file, response) {
         var content = $($(response).html()).filter('div[id=thecontent]');
  loadAttachmentAjax('', '');

Thanks to the ajax-upload plugin, uploading a file is a breeze, first of all in the onSubmit I call one of my extension named setwait, that greys out the div containing the list of attachment, command is ‘add’ and when the calls succeded the funcion onComplete clear the waiting gif, parse the result of the page (remember that my AttachAjaxAction.aspx renders the grid after it performed desidered action.), filter to find the div with id ‘the content’ and substitute all html into the div that shows the grid. The rewire function is used to wire up some events. The last line calls a funcion named loadAttachmentAjax to load the grid the first time.

function loadAttachmentAjax(cmd, atid) {
         'DomainInterventionAjaxAction.aspx #thecontent',
         { relid: $('#attachmentgrid').attr('relid'), command: cmd, attachid: atid}, 

function rewire() {
   $('img.deleteattach').click(function() {
      loadAttachmentAjax('del', $(this).attr('relid'));

These two function are really simple, the first one simply uses the load jQuery function to load the grid the first time, the rewire simply adds click functionality to the image used to remove attachment.

The result is a much more interactive page, where the user can add, remove and download attachments without a single postback.




Microsoft Ajax ScriptManager and Asp.Net MVC

I’m porting a sample application from asp.net to MVC, I used this app to make my session during last DotNetMarche event dedicated to JQuery. Now I’m writing the same sample pages in asp.net mvc to compare both technologies. In an Asp.Net page I showed how to use jQuery to do “Edit in place” of some Photo Description in an application that manages photo album. At a certain point my jQuery script will communicate with the server through an asmx webservice exposed with a ScriptManager.

Now that I’m working in asp.net MVC I’m asking if the same approach is valid, so the question is “I really need the ScriptManager to dialogate with the server?” and clearly the answer is “NO”. I’m not happy of the Script manager, because it generates a lot of Javascript code to create the infrastructure to make the SOAP request to the webserver. If it is possible I really prefer to avoid these external scripts and thanks to ASP.Net mvc this is possible. Here is the original call to the web service.

.ChangePhotoDescription(photoid, this.value,
 function(result, context, method) {
    //call succeeded
    if (result) {
    } else {
       alert('Error during save.');
function(error, context, method) {
  alert('Exception during the save.');
}, this);

With this code I’m calling the ChangePhotoDescription method of the PhotoManager webService, as usual I pass parameters, then the two callback (success and failure) and the contex. Now that I’m in asp.net MVC all this infrastructure is completely unnecessary, first of all I write this action in the PhotoManagerController.

 public JsonResult ChangePhotoDescription(Guid photoId, String newPhotoDescription)
    return Json(Services.PhotoManagerService
       .ChangePhotoDescription(photoId, newPhotoDescription));

I declare that this action accepts only post, I inserted a sleep for demo purpose (Actually I shows that when the server needs time to answer, the ui shows a waiter gif), then I call my business logic (behind a Service) and return the result to the caller as a JSonResult. I’ve completely eliminated the asmx webservice, now here is the code to call this controller.

  url: "/PhotoManager/ChangePhotoDescription",
  type: "POST",
  data: {photoId: photoid, newPhotoDescription: this.value},
  success: function (data, textStatus) {
       console.log("%o - %o", data, textStatus);
        if (data) {
         } else {
            alert('Error during save.');
  error: function(XMLHttpRequest, textStatus, errorThrown) {
     alert('Exception during the save.');
  complete: function (XMLHttpRequest, textStatus) {
  mycontext: this

Thanks to jQuery I simply use the ajax method to make a call to the server, the url is composed by /ControllerName/ActionName, with the data parameter I set POST data, that will be passed to the controller action (pay attenction to the name), then I setup the various callback, and finally I insert into ajax options object a custom property called mycontext that contains the actual span I’m modifying.

Thanks to the JsonResult ASp.Net Mvc permits to call controller’s action from client code really in a breeze.