Tuesday, December 30, 2008

Tip: CalendarExtender In Update Panel with ReadOnly TextBox

Scenario:
From time to time we want to build a date selection in a web form using the Ajax Control Toolkit's Calender Extender and to set a text box that accepts a new date everytime a user chooses a new date to ReadOnly mode so that user cannot modify the text box with unintended date format.  Most of the time, we want to put it inside an UpdatePanel so that only partial of the page is updated. The problem is, the TextBox's TextChanged event if you enable it, it will not fire if the user makes a date selection and changes the content of the text box.

Solution:
The solution to this problem is to bind the UpdatePanel "Load" event to a function. In this case, any changes in the text box content will trigger the UpdatePanel event.
In the function what we can do is to check the "__EVENTTARGET" returned from the "Request" object. For example, the following code checks whether the postback is caused by the myTextBox text box.

        protected void UpdatePanel2_Load(object sender, EventArgs e)
        {
            if (Request.Params["__EVENTTARGET"] == myTextBox.ID)
            {
                  //DO something
            }
        }

Saturday, December 6, 2008

Tips: How to compress JSON response using GZIP

The main objective for this short article is to write out the method to compress JSON response(using Web Service) and Ajax response(using normal aspx page) using IIS6 Metabase Explorer.

I have been involving in a ASP.NET web project that makes heavy use of JSON technique and Ajax concepts. The web pools for new data from a Windows 2003 web server every 30 seconds. The size of the data can vary from 100KB to 1MB. This poses performance issue for customers that have slow Internet connection.

IIS6 provides the capability to Gzip the web content before sending to the clients. Figure 1 and Figure 2 show the typical setting that i uses in order to compress JSON and Ajax response.

Figure 1: Deflate settings

Figure 2: Gzip settings.

As you can see, the main setting that is related to the JSON and Ajax response GZIP is the HcScriptFileExtensions. I have included the "aspx" and "asmx" extensions in the "Data" column.