AJAX (Asynchronous JavaScript and XML) has become so popular, thanks to Google Suggest. AJAX has opened the possibility to make more responsive and interactive web applications, bringing them closer to Windows form applications. Web developers are the bunch of guys who were happy at first. They have a new toy, which is composed of old toys they have neglected so far, and now they can make cool things with the toy. On the other hand, after getting their free account in GMail, end users demand more with their department web application. They hate postback, they want no refresh, and everything just stays there but still gets up-to-date. Is it possible?
Those demanding users are my motivation to create this custom control. Let me introduce AjaxDropDownList, my first attempt to contribute into AJAX world. AjaxDropDownList is a dropdownlist control that has the following features:
* Fetch data asynchronously in the background from a server source, with no postback. * Can trigger change event to other dropdownlists, thus generating a cascading linked dropdownlist effect. * Is encapsulated into a single control which can be easily dragged and dropped into the designer or added from the server code. * Uses common code to access the selectedItem just like a normal dropdownlist, thus can be easily integrated with other UI framework. * Compatible with Internet Explorer 6, Mozilla Firefox 1.04, and Netscape 8.02.
Although I call the control as AjaxDropDownList, it does not use XML to transfer the data. I use JSON (JavaScript Object Notation) by Douglas Crockford which is more lightweight and can be easily consumed by JavaScript as an object. I am aware of the potential and flexibility that XML offers compared to JSON. But in my case, JSON is enough to serve the requirements. How to use the control Starting from the sample project
Download and open the project in VS.NET 2003. Edit GetLookupData.aspx.cs and change the connection string to point to a valid NorthWind database. If for some reason you don’t have NorthWind database, you can download the database script from Microsoft. Just do a search on Google.
Once the solution is built successfully, run and browse the default.aspx. Evaluate if this is the control you are looking for. What the demo is about
In this demo page, we have three AjaxDropDownLists: Customers (ddlCustomers), Orders (ddlOrders) and Products (ddlProducts).
Orders dropdownlist depends on Customers. It means if we make a selection in Customers dropdownlist, then the Order dropdownlist will be filtered based on our selection. Furthermore, Products is depending on Orders. So a selection on Customers will trigger a change in Orders, and subsequently trigger a change in Products. Please note that all this happens without any postbacks.
While playing around with the dropdownlist, you may encounter a JavaScript alert box showing an error message. It could mean a lot of things, but most probably your connection to the database is not working.
Now press the Submit button and the page will finally do a postback. The selected text of each dropdownlist will be shown on the right hand side. This is to demonstrate that the standard code to access the selection in ASP.NET DropDownList still applies to AjaxDropDownList. What is in there
There are three important parts:
* AjaxDropDownList.cs
It contains the custom control. Put this file in a separate Class Library project so that we can add the control to the toolbox without any trouble. * Default.aspx
It is a sample web page that uses AjaxDropDownList controls. The controls are dropped into the designer from the Toolbox.
To add AjaxDropDownList into your toolbox, right click on the toolbox pane, and then select Add/Remove Items. It will bring up Customize Toolbox dialog. Press Browse button then select CustomControl.dll or the assembly that contains AjaxDropDownList. A big list of control with checkboxes will appear. Ensure that AjaxDropDownList is selected and close the dialog. The control will appear in the toolbox, ready to be dropped to the designer. * GetLookupData.aspx
This is the page that handles the request from xmlHttp and returns the appropriate JSON. It needs to handle two query strings: o “id” is the lookup name or identifier, e.g., Country, Currency, Order, Product, and InvoiceStatus. o “filter” (optional) describes the filter in name-value pair, e.g., Customer, ALFKI.
It is up to you how you want to implement the request handler.
Warning: GetLookupData.aspx is just a simple example which is not suitable for a product environment.
Code Walkthrough
AjaxDropDownList control uses JavaScript intensively. The JavaScript code is embedded into the control and will be injected into the response stream when the control is rendered. In order to minimize HTTP payload, the JavaScript code has been minimized using JavaScript Minifier. However, this will make debugging on the real code difficult and frustrating.
Therefore, in the sample project I provide the source code in its original format and all comments still in place. Please refer to SourceScript.aspx during this walkthrough. XMLHTTP
JavaScript code utilizes xmlHttp object to make requests to the web server either asynchronously or synchronously. As the request can be made without refreshing the page, the web page looks more responsive and interactive. The method getXMLHTTP() is called to a get reference to the xmlHttp object, regardless of how the browser implements this object. Controller
No comments:
Post a Comment