0

我正在为国家和城市开发一个级联下拉列表,目前工作正常,但我需要将other city最后一个值添加到第二个下拉列表(城市 DD),而不管国家/地区用户选择什么。我无法让它像正常的添加操作一样工作到列表中。

public CascadingDropDownNameValue[] FetchCities(string knownCategoryValues, string category)
    {
        string CountryCode;
        StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        CountryCode = strCountries["Country"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code Order by CityName ", con);
        cmd.Parameters.AddWithValue("@Code", CountryCode);
        cmd.ExecuteNonQuery();
        SqlDataAdapter dastate = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        dastate.Fill(ds);
        con.Close();
        List<CascadingDropDownNameValue> states = new List<CascadingDropDownNameValue>();
        foreach (DataRow dtRow in ds.Tables[0].Rows)
        {
            string StateID = dtRow["CityID"].ToString();
            string StateName = dtRow["CityName"].ToString();
            states.Add(new CascadingDropDownNameValue(StateName, StateID));
        }
        return states.ToArray();
    }

上面的代码是我正在按照本教程进行级联下拉的示例代码

4

2 回答 2

0

你可以在不接触你的代码的情况下做到这一点。只需在表格“城市”中添加新行

Country = "xxx" 
CityName = "ZZZ: other country"

然后更新您的 SQL:

SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code OR Country='xxx' Order by CityName ", con);

ZZZ开头只是为了确保在按 CityName 排序时始终位于末尾

于 2013-09-02T13:04:21.777 回答
0

为什么不将它作为列表传递给函数(我不喜欢返回列表)作为返回值。这样,您就可以控制在获取数据库值后列表会发生什么。

public void Fill()
{
    List<CascadingDropDownNameValue> cities = new List<CascadingDropDownNameValue>();

    FetchCities(cities, knownCategoryValues, category);

    cities.Add(new CascadingDropDownNameValue("new city", "0"));
}


public void FetchCities(List<CascadingDropDownNameValue> values, string knownCategoryValues, string category)
{
    string CountryCode;
    StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
    CountryCode = strCountries["Country"].ToString();
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code Order by CityName ", con);
    cmd.Parameters.AddWithValue("@Code", CountryCode);
    cmd.ExecuteNonQuery();
    SqlDataAdapter dastate = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    dastate.Fill(ds);
    con.Close();
    List<CascadingDropDownNameValue> states = new List<CascadingDropDownNameValue>();
    foreach (DataRow dtRow in ds.Tables[0].Rows)
    {
        string StateID = dtRow["CityID"].ToString();
        string StateName = dtRow["CityName"].ToString();
        values.Add(new CascadingDropDownNameValue(StateName, StateID));
    }
}

更新:另一个不错的方法是返回 IEnumerable< CascadingDropDownNameValue>

    public void Fill()
    {
        List<CascadingDropDownNameValue> cities = new List<CascadingDropDownNameValue>();

        cities.AddRange(FetchCities(connectionString, knownCategoryValues, category));

        cities.Add(new CascadingDropDownNameValue("new city", "0"));
    }


    public IEnumerable<CascadingDropDownNameValue> FetchCities(string connectionString, string knownCategoryValues, string category)
    {
        StringDictionary strCountries = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        string CountryCode = strCountries["Country"].ToString();

        using (SqlConnection con = new SqlConnection(connectionString))
        using (SqlCommand cmd = new SqlCommand("select * from Cities where Country=@Code Order by CityName ", con))
        {
            cmd.Parameters.Add("@Code", SqlDbType.VarChar).Value = CountryCode;
            using (SqlDataReader reader = cmd.ExecuteReader())
                while (reader.Read())
                {
                    string StateID = reader["CityID"].ToString();
                    string StateName = reader["CityName"].ToString();
                    yield return new CascadingDropDownNameValue(StateName, StateID);
                }
        }
    }

优点是它将流式传输行并在需要时将它们转换为对象。如果您只想要前 10 名,这将带来优势。它不会从 sql-server 获得更多行。(除了一些行缓存/块读取)

DataSet.Fill将在返回对象之前获取所有行。因此,如果该表包含 +1m 行。它将首先创建一个包含 1m 行的 DataTable,然后以对象的形式返回它们。

我将连接字符串传递给它的原因是允许更多打开的记录集。(每个连接一个打开的记录集)

在这种情况下,cities.AddRange将迭代所有行。仅添加前 10 名将是:

var cities = FetchCities(connectionString, knownCategoryValues, category).Take(10);
cities.AddRange(cities);

祝你好运。

于 2013-09-02T13:04:56.797 回答