uid | title | author | description | ms.author | ms.date | ms.assetid | msc.legacyurl | msc.type |
---|---|---|---|---|---|---|---|---|
web-forms/overview/ajax-control-toolkit/cascadingdropdown/using-auto-postback-with-cascadingdropdown-cs |
Using Auto-Postback with CascadingDropDown (C#) | Microsoft Docs |
wenz |
The CascadingDropDown control of the AJAX Control Toolkit extends a DropDownList control so that changes for one DropDownList loads associated values in anoth... (C#) |
riande |
06/02/2008 |
6755d8d9-14be-4a1d-86e5-1a6110f3dea8 |
/web-forms/overview/ajax-control-toolkit/cascadingdropdown/using-auto-postback-with-cascadingdropdown-cs |
authoredcontent |
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.
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.
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]
Then, a DropDownList control is required:
[!code-aspxMain]
For this list, a CascadingDropDown extender is added, providing web service URL and method information:
[!code-aspxMain]
The CascadingDropDown extender then asynchronously calls a web service with the following method signature:
[!code-csharpMain]
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]
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]
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 (Click to view full-size image)