Presetting List Entries with 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. 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):
[!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, providing web service URL and method information:
[!code-aspxMain]
1: <ajaxToolkit:CascadingDropDown ID="ccd1" runat="server"
2: ServicePath="CascadingDropdown2.vb.asmx" ServiceMethod="GetVendors"
3: TargetControlID="VendorsList" Category="Vendor" />
The CascadingDropDown extender then asynchronously calls a web service with the following method signature:
[!code-vbMain]
1: 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.
[!code-aspxMain]
1: <%@ WebService Language="VB" Class="CascadingDropDown2" %>
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 CascadingDropDown2
10: Inherits System.Web.Services.WebService
11: <WebMethod()> _
12: Public Function GetVendors(ByVal knownCategoryValues As String, ByVal category As
13: String) As CascadingDropDownNameValue()
14: Dim l As New List(Of CascadingDropDownNameValue)
15: l.Add(New CascadingDropDownNameValue("International", "1"))
16: l.Add(New CascadingDropDownNameValue("Electronic Bike Repairs & Supplies","2", True))
17: l.Add(New CascadingDropDownNameValue("Premier Sport, Inc.", "3"))
18: Return l.ToArray()
19: End Function
20: 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)
|