November 3, 2011 by Christoff Truter C# ASP.NET
In the previous part of this post we had a quick look at how to use the CascadingDropDown extender,
in this part we're going to look at other perhaps more interesting properties but more in
particular databinding (something which seems to be a common question about this
extender control).
If you ever attempted to use traditional databinding (e.g. binding your DropDownList to a
datasource) in conjunction with this extender, you might have noticed your items being
overridden, the reason being that the extender takes control of the DropdownList being extended
and performs its own databinding.
Note that it performs binding client side (even on initial binding), like seen in the following firebug screenshot:
(would have been nice if initial binding didn't perform client side calls and rather retrieved the state of the DropDownLists
as part of the main page request)
So basically we're going to have to leave it up to the extender to perform databinding.
Furthermore if we need to provide a default value (e.g. previously selected values), we can do that by setting the
SelectedValue property on the extender, like seen in the following snippet.
<asp:CascadingDropDown ID="cddlCountries" runat="server" TargetControlID="ddlCountry" Category="Country" PromptText="- Please Select -" ServicePath="~/Services/Service.asmx" ServiceMethod="GetCountries" SelectedValue='<%# Eval("CountryId") %>' LoadingText="Please wait"> </asp:CascadingDropDown> <asp:CascadingDropDown ID="cddlProvinces" runat="server" TargetControlID="ddlProvince" ParentControlID="ddlCountry" Category="Province" PromptText="- Please Select -" ServicePath="~/Services/Service.asmx" ServiceMethod="GetProvinces" SelectedValue='<%# Eval("ProvinceId") %>' LoadingText="Please wait"> </asp:CascadingDropDown> <asp:CascadingDropDown ID="cddlCities" runat="server" TargetControlID="ddlCity" ParentControlID="ddlProvince" Category="City" PromptText="- Please Select -" ServicePath="~/Services/Service.asmx" ServiceMethod="GetCities" SelectedValue='<%# Eval("CityId") %>' LoadingText="Please wait"> </asp:CascadingDropDown>
<asp:CascadingDropDown ID="cddlCities" runat="server" TargetControlID="ddlCity" ParentControlID="ddlProvince" Category="City" PromptText="- Please Select -" ServicePath="~/Services/Service2.asmx" ServiceMethod="GetCities" UseContextKey="true" ContextKey='<%# Eval("CityId") %>'">
[WebMethod] public CascadingDropDownNameValue[] GetCities( string knownCategoryValues, string category, string contextKey) { StringDictionary values = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); Int32 ProvinceId = Convert.ToInt32(values["Province"]); return City.Get(ProvinceId).Select(p => new CascadingDropDownNameValue(p.Title, p.CityId.ToString(), p.CityId == Convert.ToInt32(contextKey)) ).ToArray(); }
function pageLoad() { Sys.Net.WebRequestManager.add_completedRequest(On_completedRequest); } function On_completedRequest(sender, eventArgs) { var url = sender.get_webRequest().get_url(); if ((sender.get_statusCode() == 500) && (url == 'Services/Service.asmx/GetCities')) { alert(sender.get_statusText()); } }
October 7, 2011