KoffeeKoder


Latest Posts
  • Using FireBug Profiler to Dig Deep into MS AJAX and JQuery API
    published on 8/18/2008 10:55:15 AM (Ajax)
    Comments => 2
  • While playing with the profiling feature of FireBug I found an interesting thing! When using MS Ajax to perform Ajax it takes approx 500+ calls to a simple server side method. The calls go deep into the MS Ajax framework and invoke different methods. However, if we use JQuery to make the same call it will only take <20 calls.

    Here is the code for the MS AJAX:


     <asp:ScriptManager ID="sm1" runat="server">
        <Services>
        <asp:ServiceReference InlineScript="false" Path="~/AjaxService.asmx" />
        </Services>
        </asp:ScriptManager>


    And here it the getCustomer method:


    function getCustomer()
    {
        console.profile("start");

        DemoScreenScrape.AjaxService.GetCustomer(function(response)
        {
            var customer = response;         
            alert(customer);                
        });
        
        console.profileEnd();
    }


    And here is the JQuery API Ajax call:


    function getCustomer()
      {
        console.profile("start");
       
           $.ajax({
       type: "POST",
       contentType:"application/json",
       url: "AjaxService.asmx/GetCustomer",
       data: "{}",
       dataType:"json",
       success: function(response)
       {     
         var customer = eval('(' + response.d + ')');
         alert(customer.FirstName);
         alert(customer.LastName);    
         
       }
     });

        console.profileEnd();
        
     }



    The console.profile("start") marks the starting point of the profiling and console.profileEnd() marks the end point of the profiling. This means that all the calls between the console.Profile("start") and console.profileEnd() will be captured in the FireBug profiler.

    Here is the reading from the FireBug profiler when MS AJAX was used to call the server side method.



    And here is the call to the same server side method using JQuery API:



    So, which Ajax framework would you use in your next application?
  • Client Side GridView Paging and Soting Using SqlDataSource Control
    published on 8/17/2008 2:29:33 PM (ASP.NET)
    Comments => 0
  • ASP.NET includes a built in Ajax framework named "Client Callbacks" which is used to perform Ajax requests. The GridView control uses this feature with the data source controls to perform client side sorting and paging.

    To enable client side paging and sorting you just need to set the EnableSortingAndPagingCallbacks to true. Also, you need to make sure that the DataSourceID is set to a valid data source control like SqlDataSource, ObjectDataSource or AccessDataSource. The SqlDataSource is the easiest control to setup and requires no coding at all.

    Here is the code for GridView and SqlDataSource control.


     <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
                SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Alphabetical list of products]">
            </asp:SqlDataSource>
            <asp:GridView ID="GridView1" EnableSortingAndPagingCallbacks="true" runat="server" AllowSorting="True"
                AutoGenerateColumns="False" DataKeyNames="ProductID"
                DataSourceID="SqlDataSource1">
                <Columns>
                    <asp:BoundField DataField="ProductID" HeaderText="ProductID"
                        InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
                    <asp:BoundField DataField="ProductName" HeaderText="ProductName"
                        SortExpression="ProductName" />
                    <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice"
                        SortExpression="UnitPrice" />
                </Columns>
            </asp:GridView>



    Run the above code and you will see that you will be able to perform sorting and paging on client side.




    As you can see in the image above each time you click the GridView header to sort the size of the response is increasing. Fiddler shows that some encrypted data is sent back to the client as a response. That encrypted data is the __EVENTVALIDATION hidden field. Event Validation is a new feature in ASP.NET 2.0 which makes sure that the events are generated by only the controls which are registered to fire events.

    The only solution to this size problem I have found is to disable the event validation of the page. Please note this is a security hole so please make this decision wisely.


    <%@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="DemoScreenScrape.WebForm1" %>


    Now, if you make sorting/paging requests you will note that the size of the response is not increased.
  • Random Expectations and Unit Testing
    published on 8/16/2008 10:43:41 PM (Unit Testing)
    Comments => 0
  • When we unit test we make sure that the result is correct by comparing it to our expectations. But what if the result of the method or module is random? How do we unit test that module?

    Recently, I had to unit test a method which returned random output (strings). And I had to test that the method indeed returns a random output (string).

    Here is the method:

    [Test]
            public void TestCanGetRandomMessage()
            {          
                string message = String.Empty;
                bool isRandom = false;

                for (int i = 0; i <= 5; i++)
                {
                    int randomNumber = rand.Next(messages.CreateUserWizardTitleBarMessage.Length);
                    Console.WriteLine(randomNumber);
                    if (!String.IsNullOrEmpty(message) && message != messages.CreateUserWizardTitleBarMessage[randomNumber].message)
                        isRandom = true;
                    else
                        message = messages.CreateUserWizardTitleBarMessage[randomNumber].message;   
                }

                Assert.AreEqual(true, isRandom);

            }


    Here I am simply calling the method again and again (5 times) and comparing it to the previous string. If it returns the same thing then the method is not working else everything is OK.

    The important thing about this method is the for loop which runs 5 times. Yes, the number 5 is important! If I put 1-2 then it will cause the test to fail since, we did not get enough tries to get the random result. It is even possible with 5 that we will get the same string again and again but then the probability of this is pretty low.
Recommended Books