首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ajax:CascadingDropDown将selectedIndex设置为0

ajax:CascadingDropDown将selectedIndex设置为0
EN

Stack Overflow用户
提问于 2013-04-24 16:32:09
回答 1查看 342关注 0票数 0

我有两个ajax:CascadingDropDown,,一个是state,一个是。它的工作方式就像用户更改状态一样,然后将被重新填充新的数据。但是,问题是,一旦有了新的数据,所选的索引就会保持不变。当用户更改状态时,如何将所选索引设置为0?谢谢!

代码语言:javascript
复制
<asp:DropDownList ID="Location_State" runat="server">
</asp:DropDownList>

<ajax:CascadingDropDown ID="StateCascading" runat="server" Category="State" 
                        TargetControlID="Location_State" 
                        ServiceMethod="BindStateDropdown" 
                        ServicePath="CountyService.asmx">
</ajax:CascadingDropDown>

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

<ajax:CascadingDropDown ID="CountyCascading" runat="server" Category="County" 
                        TargetControlID="Location_County"
                        ParentControlID="Location_State"
                        ServiceMethod="BindCountyDropdown" 
                        ServicePath="CountyService.asmx">
</ajax:CascadingDropDown>

下面是填充县列表的函数

代码语言:javascript
复制
[WebMethod]
public CascadingDropDownNameValue[] BindCountyDropdown(string knownCategoryValues, string category)
{
    StringDictionary statedetails = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
    List<CascadingDropDownNameValue> countydetails = new List<CascadingDropDownNameValue>();
    string tableName = "county";
    string sqlSQL = "";
    string sqlConnStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
    SqlConnection sqlConn = new SqlConnection(sqlConnStr);
    sqlConn.Open();
    sqlSQL = "SELECT [NAME], [CNTY_FIPS] FROM " + tableName + " [county] WHERE (\[STATE_FIPS] = @STATE_FIPS order by name";
    SqlCommand cmdSql = new SqlCommand(sqlSQL, sqlConn);
    ((SqlParameter)cmdSql.Parameters.Add("@STATE_FIPS", SqlDbType.VarChar)).Value = statedetails["State"];
    SqlDataReader rdr = cmdSql.ExecuteReader();
    while (rdr.Read())
    {
        countydetails.Add(new CascadingDropDownNameValue(rdr["NAME"].ToString(), rdr["CNTY_FIPS"].ToString()));
    }
    rdr.Close();
    sqlConn.Close();
    return countydetails.ToArray();
}
EN

回答 1

Stack Overflow用户

发布于 2013-04-25 18:19:21

我自己想办法解决。一个简单而有效的解决方案。只需在0.1秒后将ddl的索引更改为0,只要ajax请求快于0.1秒!

代码语言:javascript
复制
$(function () {
        $("#MainContent_Location_State").change(function () {
            setTimeout(function () { document.getElementById('MainContent_Location_County').selectedIndex = 0; }, 100);
        });
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16197417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档