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

Filling a List Using 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. (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:

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.cs.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-csharpMain]

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

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="C#" Class="CascadingDropdown0" %>
   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 CascadingDropdown0 : System.Web.Services.WebService
  11:  {
  12:   [WebMethod]
  13:   public CascadingDropDownNameValue[] GetVendors(string knownCategoryValues, 
  14:   string category)
  15:   {
  16:   List<CascadingDropDownNameValue> l = new List<CascadingDropDownNameValue>();
  17:   l.Add(new CascadingDropDownNameValue("International", "1"));
  18:   l.Add(new CascadingDropDownNameValue("Electronic Bike Repairs & Supplies", "2"));
  19:   l.Add(new CascadingDropDownNameValue("Premier Sport, Inc.", "3"));
  20:   return l.ToArray();
  21:   }
  22:  }

Loading the page in the browser will trigger the list to be filled with three vendors.

The list is filled automatically

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

Next



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