2013-07-19 42 views
1

标记正确的方式来ASP.net CascadingDropDown的编程方式触发更改事件使用JavaScript

<asp:ScriptManager runat="server" /> 
Country Code 
<asp:TextBox ID="CoutryCodeTextBox" runat="server" onblur="selectCountry(this.id);"> 
</asp:TextBox> 

<asp:DropDownList ID="CountryDropDownList" runat="server"> 
</asp:DropDownList> 

<ajaxToolkit:CascadingDropDown 
    ID="CountryDropDownListCascadingDropDown" runat="server" 
    TargetControlID="CountryDropDownList" 
    Category="Country" 
    ServiceMethod="GetCountries" 
    ServicePath="~/CountryData.asmx" 
    LoadingText="Loading ..." 
    PromptText="SELECT"> 
</ajaxToolkit:CascadingDropDown> 


<asp:DropDownList ID="CityDropDownList" runat="server"> 
</asp:DropDownList> 
<ajaxToolkit:CascadingDropDown 
    ID="CityDropDownListCascadingDropDown" runat="server" 
    ParentControlID="CountryDropDownList" 
    TargetControlID="CityDropDownList" 
    Category="City" ServiceMethod="GetCities" 
    ServicePath="~/CountryData.asmx" 
    LoadingText="Loading ..." 
    PromptText="SELECT"> 
</ajaxToolkit:CascadingDropDown> 

Web服务(〜/ CountryData.asmx)

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
[ScriptService] 
public class CountryData : System.Web.Services.WebService 
{ 
    [WebMethod] 
    public CascadingDropDownNameValue[] GetCountries(string knownCategoryValues, string category) 
    { 
     List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); 

     values.Add(new CascadingDropDownNameValue("United States", "US")); 
     values.Add(new CascadingDropDownNameValue("Canada", "CA")); 

     return values.ToArray(); 
    } 

    [WebMethod] 
    public CascadingDropDownNameValue[] GetCities(string knownCategoryValues, string category) 
    { 
     StringDictionary kv = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues); 
     string country = kv["Country"]; 

     List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>(); 

     switch (country) 
     { 
      case "US": 
       values.Add(new CascadingDropDownNameValue("California", "CA")); 
       values.Add(new CascadingDropDownNameValue("New York", "NY")); 
       break; 
      case "CA": 
       values.Add(new CascadingDropDownNameValue("Toronto", "TO")); 
       values.Add(new CascadingDropDownNameValue("Montreal", "MO")); 
       break; 
     } 

     return values.ToArray(); 
    } 

} 

jQuery的

var selectCountry = function (id) 
    { 
     var countryCodeTextBox = $("#" + id); 
     var countryDropDownList = $("#CountryDropDownList"); 

     countryDropDownList.val(countryCodeTextBox.val()); 
     countryDropDownList.change(); 
    } 

的javascript函数更改CountryDropDownList的选定值。但是,级联控件CityDropDownList不会自动填充。

什么是触发使用jQuery,使相关的控制(S)能自动级联父控件更改事件的正确方法?

+0

请问,当你在这里使用的方法是触发:http://stackoverflow.com/questions/499405/change-the-selected-value-of-a-drop-down-list-with-jquery –

+0

这正是我在做。它没有帮助。 –

回答

4

根据你,my answer on How can I trigger an onchange event manually?也解决了你的问题:

有几个方法可以做到这一点。如果onchange听者 是通过element.onchange属性设置功能,你不 不屑关于该事件的对象或起泡/传播,最简单的方法 是只调用该函数:

element.onchange(); 

如果您需要它模拟全是真实发生的,或者如果你通过HTML属性设置 事件或addEventListener/attachEvent,你 需要做一些特征检测的正确触发事件:

if ("createEvent" in document) { 
    var evt = document.createEvent("HTMLEvents"); 
    evt.initEvent("change", false, true); 
    element.dispatchEvent(evt); 
} 
else 
    element.fireEvent("onchange"); 
+1

你是个摇滚明星安迪。 :d –

+0

恕我直言,这应该由jQuery的实现。 –

相关问题