【问题标题】:cascading dropdownlist without reloading the page in asp.net级联下拉列表而不在asp.net中重新加载页面
【发布时间】:2013-11-01 22:35:26
【问题描述】:

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

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

简而言之,我的问题是:从 asp.net 的下拉列表中选择国家、州和城市,而无需重新加载页面。

这里我给你aspx部分。

请帮帮我。

CountryDropDown

<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>

StateDropDown

<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>

【问题讨论】:

  • 查看 .NET Web 方法。基本上是一个 ajax 调用来执行代码隐藏功能。 Here is an example
  • 您的代码看起来不错。你遇到了什么错误?
  • 我没有收到任何错误,但我无法实现所需的功能。
  • 让您的代码在没有 Ajax 工具包的情况下工作。一旦您使用完整的帖子使下拉菜单正常运行,然后将它们包装在更新面板中并配置您的触发器。

标签: c# javascript asp.net ajax ajaxcontroltoolkit


【解决方案1】:

哇,你居然把解释问题搞得一团糟...... 但我会尽力提供帮助。首先,第一个 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 中必须为真,才能进行异步回发,

尝试一个一个删除 DDL,只从 2 开始,然后继续前进。

【讨论】:

    【解决方案2】:

    首先创建一个 Web 服务来为您的下拉列表检索数据

    创建网络服务:CascadingDropdown.asmx 在您的 CascadingDropdown.asmx.cs 中编写代码从数据库中检索 Country、State 和 City 的数据,看看我是这样做的,您可以这样做,我使用实体框架从数据库中获取数据。

        [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();
        }
    

    然后在你的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>
    

    我正在做的是,当我们选择国家/地区下拉列表时,我将国家/地区 id 传递给 FetchStates 网络方法,该方法位于我们的 CascadingDropdown.asmx.cs Web 服务中,以根据国家/地区 id 获取状态,同样适用城市,将州 ID 传递给 FetchCities 网络方法以获取城市。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2023-03-25
      • 2020-07-12
      • 2010-10-14
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多