The CascadingDropDown Extender
The CascadingDropDown extender can be attached to a DropDownList control to automatically populate it based on the current selection of one or more parent DropDownList controls.
The CascadingDropDown extender is designed to fit in a relatively common scenario in which the contents of one drop-down list depends on the selection of another list. In this way, you don't need to transfer to the client the entire data set from which a child list can select a subset of items to display in accordance with the selection on its parent. For example, suppose you want the user to select a country and a city in that country. To minimize data transfer and provide a friendlier user interface, you might want to keep the city list empty until a selection is made on the country list. When a country is selected, you get back to the server to download the list of cities available for that country. The CascadingDropDown extender simplifies this scenario by injecting some glue code in the client page and also making some assumptions on the structure of your page code.
All the logic about the contents of the set of DropDownList controls is expected to live on a Web service. The Web service, in turn, can use any suitable method for storing and looking up any relevant data. The Web service, though, is somewhat forced to a contracted schema. In particular, it needs to have a method with the following signature:
[System.Web.Services.WebMethod]
public CascadingDropDownNameVa1ue[] GetDropDownContents( string knownCategoryValues, string category)
Note You can programmatically control any settings on the target properties of Atlas extenders. You use the TargetProperties collection exposed by each extender and retrieve a particular property through a 0-based index.
protected void Page_Load(object sender, EventArgs e) {
Drag0ver1ay1.TargetProperties[0].Enab1ed = true;
The sample code enables the target control of the first DragOverlay property to be floated around the page.
The name of the method can vary, of course. The CascadingDropDownNameValue type is an internal collection type that is designed to contain the name/value items to show in the dropdown list. Each drop-down list bound to the extender belongs to a category:
<act:CascadingDropDown ID="CascadingDropDown1" runat="server">
<act:CascadingDropDownProperties TargetControlID="DropDownList1" Category="Country"
PromptText="Please select a country" ServiceMethod="GetDropDownContentsPageMethod" /> <act:CascadingDropDownProperties TargetControlID="DropDownList2" Category="City"
PromptText="Please select a city" LoadingText="Please, wait ..." ServicePath="CityFinderService.asmx" ServiceMethod="GetDropDownContents" ParentControlID="DropDownList1" /> </act:CascadingDropDown>
The content of the Category property is any name that helps the Web service method to understand what kind of data should be retrieved and the meaning of the input arguments. The PromptText property sets any text that you want to display in the drop-down list when no selection is currently made and the control is typically disabled. The LoadingText property indicates any text that has to be displayed while the drop-down list is being populated. The ServiceMethod property indicates the method to call to fill in the list control. If no ServicePath is specified, the method is assumed to be a page method. Finally, ParentControlID creates a hierarchy and designates a list to be the child of another.
Each time the selection changes in a parent DropDownList control, the extender makes a call to the Web service and retrieves the list of values for the next DropDownList in the hierarchy. If no selection is currently made, the extender automatically disables the control. (See Figure 4-14.)
Figure 4-14 The CascadingDropDown extender in action on two drop-down lists
Post a comment