"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Presetting List Entries with CascadingDropDown (C#)

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):

[!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.cs.asmx" ServiceMethod="GetVendors"
   3:   TargetControlID="VendorsList" Category="Vendor" />

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

[!code-csharpMain]

   1:  public CascadingDropDownNameValue[] MethodNameHere(string knownCategoryValues, string category)

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="C#" Class="CascadingDropdown2" %>
   2:  using System.Web.Script.Services;
   3:  using AjaxControlToolkit;
   4:  using System;
   5:  using System.Web;
   6:  using System.Web.Services;
   7:  using System.Web.Services.Protocols;
   8:  using System.Collections.Generic;
   9:  [ScriptService]
  10:  public class CascadingDropdown2 : System.Web.Services.WebService
  11:  {
  12:   [WebMethod]
  13:   public CascadingDropDownNameValue[] GetVendors(string knownCategoryValues, string
  14:   category)
  15:   {
  16:   List<CascadingDropDownNameValue> l = new List<CascadingDropDownNameValue>();
  17:   l.Add(new CascadingDropDownNameValue(
  18:   "International", "1"));
  19:   l.Add(new CascadingDropDownNameValue(
  20:   "Electronic Bike Repairs & Supplies", "2", true));
  21:   l.Add(new CascadingDropDownNameValue(
  22:   "Premier Sport, Inc.", "3"));
  23:   return l.ToArray();
  24:   }
  25:  }

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

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

Previous Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/ajax-control-toolkit/cascadingdropdown/presetting-list-entries-with-cascadingdropdown-cs.htm
< THANKS ME>