Filling a List Using CascadingDropDown (VB)
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.) The first challenge to solve is to actually fill a dropdown list using this control.
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.) The first challenge to solve is to actually fill a dropdown list using this control.
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):
[!code-aspxMain]
1: <asp:ScriptManager ID="asm" runat="server" />
Then, a DropDownList control is required:
[!code-aspxMain]
1: <div>
2: Vendor: <asp:DropDownList ID="VendorsList" runat="server" />
3: </div>
For this list, a CascadingDropDown extender is added. It will send an asynchronous request to a web service which will then return a list of entries to be displayed in the list. For this to work, the following CascadingDropDown attributes need to be set:
ServicePath
: URL of a web service delivering the list entriesServiceMethod
: Web method delivering the list entriesTargetControlID
: ID of the dropdown listCategory
: Category information that is submitted to the web method when calledPromptText
: Text displayed when asynchronously loading list data from the server
Here is the markup for the CascadingDropDown
element. The only difference between C# and VB is the name of the associated web service:
[!code-aspxMain]
1: <ajaxToolkit:CascadingDropDown ID="ccd1" runat="server"
2: ServicePath="CascadingDropdown0.vb.asmx" ServiceMethod="GetVendors"
3: TargetControlID="VendorsList" Category="Vendor" />
The JavaScript code coming from the CascadingDropDown
extender calls a web service method with the following signature:
[!code-vbMain]
1: Public Function MethodNameHere(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
So the important aspect is that the method needs to return an array of type CascadingDropDownNameValue
(defined by the ASP.NET AJAX Control Toolkit). In the CascadingDropDownNameValue
contructor, first the list entry’s text and then its value must be provided, just as <option value="VALUE">NAME</option>
would do in HTML. Here is some sample data:
[!code-aspxMain]
1: <%@ WebService Language="VB" Class="CascadingDropDown0" %>
2: Imports System.Web.Script.Services
3: Imports AjaxControlToolkit
4: Imports System.Web
5: Imports System.Web.Services
6: Imports System.Web.Services.Protocols
7: Imports System.Collections.Generic
8: <ScriptService()> _
9: Public Class CascadingDropDown0
10: Inherits System.Web.Services.WebService
11: <WebMethod()> _
12: Public Function GetVendors(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
13: Dim l As New List(Of CascadingDropDownNameValue)
14: l.Add(New CascadingDropDownNameValue("International", "1"))
15: l.Add(New CascadingDropDownNameValue("Electronic Bike Repairs & Supplies","2"))
16: l.Add(New CascadingDropDownNameValue("Premier Sport, Inc.", "3"))
17: Return l.ToArray()
18: End Function
19: End Class
Loading the page in the browser will trigger the list to be filled with three vendors.
The list is filled automatically (Click to view full-size image)
|