Language

Presetting List Entries with CascadingDropDown (VB)

By Christian Wenz|

Download Code or Download PDF

The CascadingDropDown control in the AJAX Control Toolkit extends a DropDownList control so that changes in one DropDownList loads associated values in another DropDownList. With a little bit of code it is possible that a list element is preselected once the data has been dynamically loaded.

Overview

The CascadingDropDown control in the AJAX Control Toolkit extends a DropDownList control so that changes in one DropDownList loads associated values in another DropDownList. (For instance, one list provides a list of US states, and the next list is then filled with major cities in that state.) With a little bit of code it is possible that a list element is preselected once the data has been dynamically loaded.

Steps

In order to activate the functionality of ASP.NET AJAX and the Control Toolkit, the ScriptManager control must be put anywhere on the page (but within the <form> element):

<asp:ScriptManager ID="asm" runat="server" />

Then, a DropDownList control is required:

<div> Vendor: <asp:DropDownList ID="VendorsList" runat="server"/> </div>

For this list, a CascadingDropDown extender is added, providing web service URL and method information:

<ajaxToolkit:CascadingDropDown ID="ccd1" runat="server" ServicePath="CascadingDropdown2.vb.asmx" ServiceMethod="GetVendors" TargetControlID="VendorsList" Category="Vendor" />

The CascadingDropDown extender then asynchronously calls a web service with the following method signature:

Public Function MethodNameHere(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()

The method returns an array of type CascadingDropDown value. The type's constructor expects first the list entry's caption and then the value (HTML value attribute). If the third argument is set to true, the list element is automatically selected in the browser.

<%@ WebService Language="VB" Class="CascadingDropDown2" %> Imports System.Web.Script.Services Imports AjaxControlToolkit Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols Imports System.Collections.Generic <ScriptService()> _ Public Class CascadingDropDown2 Inherits System.Web.Services.WebService <WebMethod()> _ Public Function GetVendors(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue() Dim l As New List(Of CascadingDropDownNameValue) l.Add(New CascadingDropDownNameValue("International", "1")) l.Add(New CascadingDropDownNameValue("Electronic Bike Repairs & Supplies","2", True)) l.Add(New CascadingDropDownNameValue("Premier Sport, Inc.", "3")) Return l.ToArray() End Function End Class

Loading the page in the browser will fill the dropdown list with three vendors, the second one being preselected.

The list is filled and preselected automatically (Click to view full-size image)

This article was originally created on June 2, 2008

Author Information

Christian Wenz

Christian Wenz – Christian Wenz is an author, trainer, and consultant. His main focus of working and writing is on web technologies and security. Christian has written or co-written over 100 books for various publishers. He works with both open source and closed source web technologies. This leads to the unusual situation that he has both been awarded a Microsoft MVP for ASP/ASP.NET and is listed in Zend's Who is Who of PHP. He is also listed in Mozilla's credits (about:credits) and is considered an expert in browser-agnostic JavaScript.