我有两个 ajax:CascadingDropDown ,一个是 州 ,一个是 县 。它就好像用户更改 状态 一样,然后 县 将重新填充新数据。但问题是,一旦 县 拥有新数据,所选索引将保持不变。当用户更改 状态 时,如何将所选索引设置为0?谢谢!
<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>
这是填充县名单的功能
[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();
}
答案 0 :(得分:0)
我自己提供解决方案。一个简单但有效的解决方案 只要ajax请求快于0.1秒,只需在0.1秒后将ddl的索引更改为0!
$(function () {
$("#MainContent_Location_State").change(function () {
setTimeout(function () { document.getElementById('MainContent_Location_County').selectedIndex = 0; }, 100);
});
});