【发布时间】:2018-12-08 06:51:42
【问题描述】:
我使用 ajax 级联下拉列表绑定下拉列表并使用 web 方法,它运行良好,我能够将国家和州 ID 保存在数据库中现在我想将保存的 ID 绑定到填充方法的下拉列表中,我该如何实现这一点。
Aspx 代码如下:::
<li>
<asp:DropDownList ID="ddlCountry" runat="server" TabIndex="11"></asp:DropDownList>
<ajax:CascadingDropDown ID="csdCountry" runat="server" Category="Country" TargetControlID="ddlCountry" PromptText="-- Select Country --" LoadingText=" [Loading Countries...]" ServiceMethod="FetchCountries" ServicePath="~/AjaxCascadingDropDown.asmx"></ajax:CascadingDropDown>
</li>
<li>
<asp:DropDownList ID="ddlState" runat="server" TabIndex="12"></asp:DropDownList>
<ajax:CascadingDropDown ID="csdState" runat="server" ParentControlID="ddlCountry" Category="State" TargetControlID="ddlState" PromptText="-- Select State --" LoadingText="[Loading States...]" ServiceMethod="FetchStates" ServicePath="~/AjaxCascadingDropDown.asmx"></ajax:CascadingDropDown>
</li>
Web 方法代码如下:::
[WebMethod]
public CascadingDropDownNameValue[] FetchCountries(string knownCategoryValues, string category)
{
GetLookupResponse countryLookupResponse = commonService.GetLookup("Country");
List<CascadingDropDownNameValue> countries = new List<CascadingDropDownNameValue>();
foreach (var countryData in countryLookupResponse.LookupItems)
{
string CountryID = countryData.ID.ToString();
string CountryName = countryData.Description.ToString();
countries.Add(new CascadingDropDownNameValue(CountryName, CountryID));
}
return countries.ToArray();
}
[WebMethod]
public CascadingDropDownNameValue[] FetchStates(string knownCategoryValues, string category)
{
int countryId;
StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
countryId = Convert.ToInt32(strCountries["Country"]);
GetLookupResponse stateLookupResponse = commonService.GetLookup("State");
List<CascadingDropDownNameValue> states = new List<CascadingDropDownNameValue>();
foreach (var StateData in stateLookupResponse.LookupItems.Where(id => id.dependencyID == countryId))
{
string StateID = StateData.ID.ToString();
string StateName = StateData.Description.ToString();
states.Add(new CascadingDropDownNameValue(StateName, StateID));
}
return states.ToArray();
}
填充下拉列表代码 :::
ddlCountry.SelectedValue = address.Country.ID.ToString();
ddlState.SelectedValue = address.State.ID.ToString();
ddlCity.SelectedValue = address.City.ID.ToString();
【问题讨论】:
标签: c# jquery asp.net ajax webmethod