C# 在asp.net中不重新加载页面的级联dropdownlist

C# 在asp.net中不重新加载页面的级联dropdownlist,c#,javascript,asp.net,ajax,ajaxcontroltoolkit,C#,Javascript,Asp.net,Ajax,Ajaxcontroltoolkit,您好,我正在开发一个asp.net web应用程序,因为我正在创建一个注册表。在注册表格页面上,我有三个下拉列表,分别是国家、州、城市。 所以当用户选择任何国家时,该国的州将显示在州的下拉列表中,当用户从州下拉列表中选择州时,他可以在下拉列表中看到城市列表 我已经实现了该功能,但当用户在dropdownlist中选择值时,会发生回发。 在我的例子中,我不想在用户选择国家或州时重新加载页面,所以我尝试使用ajax工具包实现相同的功能。但我无法使用ajax实现相同的功能 所以简单地说,我的问题是:在

您好,我正在开发一个asp.net web应用程序,因为我正在创建一个注册表。在注册表格页面上,我有三个下拉列表,分别是国家、州、城市。 所以当用户选择任何国家时,该国的州将显示在州的下拉列表中,当用户从州下拉列表中选择州时,他可以在下拉列表中看到城市列表

我已经实现了该功能,但当用户在dropdownlist中选择值时,会发生回发。 在我的例子中,我不想在用户选择国家或州时重新加载页面,所以我尝试使用ajax工具包实现相同的功能。但我无法使用ajax实现相同的功能

所以简单地说,我的问题是:在asp.net中从dropdownlist中选择国家、州和城市,而无需重新加载页面

这里我给你的aspx部分

请帮帮我

乡村下拉列表

<asp:DropDownList ID="DropDownListCountry" runat="server" Enabled="false"  
        OnSelectedIndexChanged="DropDownListCountry_OnSelectedIndexChanged" 
        AutoPostBack ="false">
     <asp:ListItem>India</asp:ListItem>
     <asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>

印度
其他
状态下拉列表

<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="DropDownListCountry" EventName="OnSelectedIndexChanged" />
    </Triggers>
    <ContentTemplate>
        <asp:DropDownList runat="server" ID="DropDownListState"  Enabled="false"
           OnSelectedIndexChanged="DropDownListState_OnSelectedIndexChanged">
        </asp:DropDownList>
    </ContentTemplate>
</asp:UpdatePanel>

哇,你解释错了什么,弄得一团糟。。。。 但是我会尽我最大的努力去帮助你。首先,第一个DDL的定义如下:

<asp:DropDownList ID="Contries" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Contries_SelectedIndexChanged">
    <asp:ListItem Text="country1" />
    <asp:ListItem Text="country2" />
</asp:DropDownList>

第二个DDL:

   <asp:UpdatePanel runat="server" >
    <ContentTemplate> 
        <asp:DropDownList ID="States" runat="server" AutoPostBack="true">
    <asp:ListItem Text="state1" />
    <asp:ListItem Text="state2" />


</asp:DropDownList>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Contries" EventName="OnSelectedIndexChanged" />
    </Triggers>
</asp:UpdatePanel>

以此类推,自动回发必须在DDL中为true,该DDL应执行异步回发


尝试逐个删除DDL,仅从2开始,然后向前移动。

首先创建一个web服务来检索DropDownList的数据

创建Web服务:CascadingDropdown.asmx 在您的CascadingDropdown.asmx.cs编写代码中,从数据库中检索国家、州和城市的数据,请看我是如何做到的,您可以这样做,我使用实体框架从数据库中获取数据

    [WebMethod]
    public CascadingDropDownNameValue[] FetchCountries()
    {
        GetLookupResponse countryLookupResponse = commonService.GetLookup("Country");
        List<CascadingDropDownNameValue> countries = new List<CascadingDropDownNameValue>();
        foreach (var dbCountry in countryLookupResponse.LookupItems)
        {
            string countryID = dbCountry.ID.ToString();
            string countryName = dbCountry.Description.ToString();
            countries.Add(new CascadingDropDownNameValue(countryName, countryID));
        }
        return countries.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] FetchStates(string knownCategoryValues)
    {
        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 dbState in stateLookupResponse.LookupItems.Where(id => id.DependencyID == countryID))
        {
            string stateID = dbState.ID.ToString();
            string stateName = dbState.Description.ToString();
            states.Add(new CascadingDropDownNameValue(stateName, stateID));
        }
        return states.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] FetchCities(string knownCategoryValues)
    {
        int stateID;
        StringDictionary strStates = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        stateID = Convert.ToInt32(strStates["State"]);
        GetLookupResponse cityLookupResponse = commonService.GetLookup("City");
        List<CascadingDropDownNameValue> cities = new List<CascadingDropDownNameValue>();
        foreach (var dbCity in cityLookupResponse.LookupItems.Where(id => id.DependencyID == stateID))
        {
            string cityID = dbCity.ID.ToString();
            string cityName = dbCity.Description.ToString();
            cities.Add(new CascadingDropDownNameValue(cityName, cityID));
        }
        return cities.ToArray();
    }
[WebMethod]
public CascadingDropDownNameValue[]FetchCountries()
{
GetLookupResponse countryLookupResponse=commonService.GetLookup(“国家”);
列表国家=新列表();
foreach(var dbCountry in countryLookupResponse.LookupItems)
{
字符串countryID=dbCountry.ID.ToString();
字符串countryName=dbCountry.Description.ToString();
添加(新的CascadingDropDownNameValue(countryName,countryID));
}
返回国家。ToArray();
}
[网络方法]
public CascadingDropDownNameValue[]获取状态(字符串knownCategoryValues)
{
国际国家ID;
StringDictionary strCountries=AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
countryID=转换为32(strCountries[“Country”]);
GetLookupResponse stateLookupResponse=commonService.GetLookup(“状态”);
列表状态=新列表();
foreach(stateLookupResponse.LookupItems.Where(id=>id.DependencyID==countryID))中的变量dbState)
{
字符串stateID=dbState.ID.ToString();
字符串stateName=dbState.Description.ToString();
Add(新的CascadingDropDownNameValue(stateName,stateID));
}
返回状态。ToArray();
}
[网络方法]
public CascadingDropDownNameValue[]获取城市(字符串KnownCategoryValue)
{
int stateID;
StringDictionary strStates=AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
stateID=Convert.ToInt32(strStates[“State”]);
GetLookupResponse cityLookupResponse=commonService.GetLookupResponse(“城市”);
列表城市=新列表();
foreach(cityLookupResponse.LookupItems.Where中的变量dbCity(id=>id.DependencyID==stateID))
{
字符串cityID=dbCity.ID.ToString();
字符串cityName=dbCity.Description.ToString();
添加(新的cascadingdropdownname值(cityName,cityID));
}
返回城市。ToArray();
}
然后在您的aspx文件中,您需要在下面页面的顶部注册AjaxControlToolkit

,如果尚未安装AjaxControlToolkit,请从Nuget软件包安装它。 然后您的下拉列表代码:

<label class="col-sm-3 col-form-label required">Country</label>
<div class="col-sm-9">
    <asp:DropDownList ID="ddlCountry" runat="server" CssClass="form-control"></asp:DropDownList>
    <ajax:CascadingDropDown ID="csdCountry" runat="server"
        Category="Country"
        TargetControlID="ddlCountry"
        LoadingText="Loading Countries..."
        ServiceMethod="FetchCountries"
        ServicePath="~/CascadingDropdown.asmx"></ajax:CascadingDropDown>
</div>

<label class="col-sm-3 col-form-label required">State</label>
<div class="col-sm-9">
    <asp:DropDownList ID="ddlState" runat="server" CssClass="form-control"></asp:DropDownList>
    <ajax:CascadingDropDown ID="csdState" runat="server"
        ParentControlID="ddlCountry"
        Category="State"
        TargetControlID="ddlState"
        LoadingText="Loading States..."
        ServiceMethod="FetchStates"
        ServicePath="~/CascadingDropdown.asmx"></ajax:CascadingDropDown>
</div>

<label class="col-sm-3 col-form-label required">City</label>
<div class="col-sm-9">
    <asp:DropDownList ID="ddlCity" runat="server" CssClass="form-control"></asp:DropDownList>
    <ajax:CascadingDropDown ID="csdCity" runat="server"
        ParentControlID="ddlState"
        Category="City"
        TargetControlID="ddlCity"
        LoadingText="Loading Cities..."
        ServiceMethod="FetchCities"
        ServicePath="~/CascadingDropdown.asmx"></ajax:CascadingDropDown>
</div>
国家
陈述
城市
我在这里做的是,当我们从country dropdownlist中选择country时,我将country id传递给FetchStates webmethod,该webmethod位于我们的CascadingDropdown.asmx.cs web服务中,以基于country id获取州,对于city也是如此,将state id传递给FetchCities webmethod以获取城市


希望有帮助。

研究.NET web方法。基本上是一个ajax调用来执行代码隐藏函数。你的代码看起来不错。您遇到了什么错误?我没有遇到任何错误,但我无法实现所需的功能。在没有Ajax工具包的情况下,让您的代码正常工作。一旦您使用完整的回发功能使下拉列表正常工作,然后将其包装在更新面板中并配置触发器。