The AJAX Cascading Dropdown extender control can also be used with SQL Server Database to populate the target dropdown controls with data retrieved form the database using web service method. Here we will use the same example of Northwind database in which first drop will display the categories retrieved from the categories table of Northwind and on selected index event will call the web service method to populate the other dropdown control with the products that belong to the selected category.
In this sample example for AJAX cascading dropdown control to work with SQL Database we will see how the Category property plays an important role to retrieve the related results from the database tables.
When ParentControlID is passed to the Cascading Dropdown extender it sends the value of the selected item of parent dropdown control along with its key name defined in the Category Property of Parent dropdown control. In the sample code you will see that categoryID of the selected item of categories dropdown (Parent Dropdown control) is sent to the web method when any categoryName is selected. It passed the combination of Category Property value and selected item value of Parent Dropdown control as:
category:3 or category:5
In the above values category is the key name used in the Category property of first dropdown control of the Cascading Dropdown sample. Numeric value is the categoryID of the selected item. knownCategoryValues parameter of the web method provides the value for the selected item of Parent Dropdown control.
To use the above value you can declare a variable of type StringDictionary and parse it using:
StringDictionary categoryValues = AjaxControlToolkit.CascadingDropDown. ParseKnownCategoryValuesString(knownCategoryValues);
StringDictionary class belongs to the Specialized namespace of System.Collections
List<> type Generic collection can be used to accumulate the results retrieved using SqlDataAdapter:
List<AjaxControlToolkit.CascadingDropDownNameValue> cascadingValues = new List<AjaxControlToolkit.CascadingDropDownNameValue>();
In the web method used for web service of cascading dropdown extender control You can use the different names for the web methods but you have to use the same name and datatype of the web method parameters and return type as used in the provided web service sample. E.g.:
public AjaxControlToolkit.CascadingDropDownNameValue[] GetDropDownCategories(string knownCategoryValues, string category)
You have to set the EnableEventValition="false" in the <% @Page %> header section of ASP.Net web page to enable the Asynchronous postback triggers of UpdatePanel.
You can download the free sample code for AJAX Cascading Dropdown Example using SQL Database here…
ajax cascading dropdown database.zip (4.63 kb)
Currently rated 2.0 by 1 people
Tags: asp.net 2.0, ajax, ajax cascading dropdown control, asp.net ajax control toolkit, cascading dropdown extender, ajax cascading dropdown xml, ajax cascading dropdown database, ajax cascading dropdown web service
8/23/2008 8:57:28 PM