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

Using Auto-Postback 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. However when using the CascadingDropDown control, ASP.NET’s DropDownList control’s AutoPostBack feature does not work, since asynchronously loading data into the list generates an (unnecessary) postback itself. With some JavaScript code, this effect can be avoided.

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.) However when using the CascadingDropDown control, ASP.NET’s DropDownList control’s AutoPostBack feature does not work, since asynchronously loading data into the list generates an (unnecessary) postback itself. With some JavaScript code, this effect can be avoided.

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="CascadingDropdown3.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).

[!code-aspxMain]

   1:  <%@ WebService Language="VB" Class="CascadingDropDown3" %>
   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 CascadingDropDown3
  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 fill the dropdown list with three vendors, the second one being preselected. Also, ASP.NET defines the __doPostBack() JavaScript method. Once the page has been loaded, this JavaScript call is added to the dropdown list, but only if there are elements in it. If there are no elements in the list, the Control Toolkit is currently loading them, so the JavaScript code uses a timeout and tries again in a half second.

[!code-htmlMain]

   1:  <script type="text/javascript">
   2:   function pageLoad()
   3:   {
   4:   addAutoPostBack();
   5:   }
   6:   function addAutoPostBack()
   7:   {
   8:   if ($get("VendorsList").options.length > 0)
   9:   {
  10:   $get("VendorsList").setAttribute("onchange","javascript:setTimeout('__doPostBack(\\'VendorsList\\',\\'\\')', 0)");
  11:   }
  12:   else
  13:   {
  14:   setTimeout("addAutoPostBack()", 500);
  15:   }
  16:   }
  17:  </script>

This way, a postback is only executed when there are actually elements in the list and the user selects an entry.

Selecting a list element causes a postback

Selecting a list element causes a postback (Click to view full-size image)

Previous





Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/ajax-control-toolkit/cascadingdropdown/using-auto-postback-with-cascadingdropdown-vb.htm
<SITEMAP>  <MVC>  <ASP>  <NET>  <DATA>  <KIOSK>  <FLEX>  <SQL>  <NOTES>  <LINUX>  <MONO>  <FREEWARE>  <DOCS>  <ENG>  <CHAT ME>  <ABOUT ME>  < THANKS ME>