0

我有一个dropdownlist邮政编码。

这是一个非常大的列表,从数据库加载需要很长时间。因此,首先我正在寻找最佳解决方案,以便在首次加载网站时尝试缓存或保存这些数据,以便我可以在需要时使用它。我尝试了一个列表和字典,然后datasource在后面设置了代码,但它不允许我设置选定的值。

它一直告诉我它不能绑定到没有 no 的控件datasource。让事情变得更加困难的dropdownlist是里面的一个formview

我只是不确定解决这个问题的最佳方法。我datasource在 created 方法中设置 and 值,formview但我选择的值来自formview datasource.

 private Dictionary<int, string> CreateZipcodeList()
{
    string connStr = ConfigurationManager.ConnectionStrings["KlamathAlgaeEntityDevConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(connStr);
    Dictionary<int,string> ziplist = new Dictionary<int,string>();

    //Create CoreEntity Record
    SqlCommand cmd1 = new SqlCommand(@"select zipcode.ZipCodeId, zipcode.zip + ', ' + city.name + ', ' + ISNULL(GeographicState.name,'') + ', ' + Country.name as zipstring
                                    from zipcode left outer join City on ZipCode.CityId = city.CityId
                                    left outer join GeographicState on ZipCode.GeographicStateId = GeographicState.GeographicStateId
                                    left outer join Country on ZipCode.CountryId = Country.CountryId
                                    order by country.name, GeographicState.name, city.name",
                                    conn);

    try
    {
        conn.Open();

        SqlDataReader reader = cmd1.ExecuteReader(CommandBehavior.CloseConnection);
        while (reader.Read())
        {
            if (!reader.IsDBNull(0))
            {
                ziplist.Add(reader.GetInt32(0), reader["zipstring"].ToString());
            }
        }
    }

    finally
    {
        if (conn != null)
        {
            conn.Close();
        }
    }
    return ziplist;
}

protected void AddressForm_ItemCreated(Object sender, EventArgs e)
{


         ((DropDownList)AddressForm.FindControl("Zipcodeddl")).DataSource = CreateZipcodeList();
        ((DropDownList)AddressForm.FindControl("Zipcodeddl")).DataTextField = "Value";
        ((DropDownList)AddressForm.FindControl("Zipcodeddl")).DataValueField = "Key";
        ((DropDownList)AddressForm.FindControl("Zipcodeddl")).DataBind();
        //((DropDownList)AddressForm.FindControl("Zipcodeddl")).SelectedValue = Eval("zipcodeid").ToString();
}

这可以很好地填充下拉列表,但是当我尝试设置所选值时,它说控件不是数据绑定的。这也不会将字典存储在任何地方,因此我需要在需要时调用该函数来加载字典。

4

1 回答 1

0

好的,下拉的记录太多了。为什么不让用户输入 5 个数字字符,然后按该数字在您的数据库中进行搜索。我想不出任何理由您必须将整个邮政编码列表放在下拉列表中,因为没有人会手动单击以找到他们想要的那个。我不认为它服务于一个有用的目的,但如果有一个让我知道,我会尝试找出一个解决方案。

邮局就是这样处理的。

更新

为了完成这项工作,您将拥有一个文本框,用户可以在其中输入他们认为的邮政编码。给他们一个按钮以单击以搜索该邮政编码。

对您的数据库执行 sql 查询以查找该 Zipcode.Zip,如果找到,则带回您需要的所有数据。

如果没有找到,我会开始删除最后一个字符并使用“Zipcode.Zip like '%modifiedZipCode%'”的修改查询进行另一次搜索,并带回最接近的 10 个左右的选项,并将它们放在下拉式菜单。

如果您删除一个数字并且数据库找不到它,您可以尽可能多地删除字符,然后重新运行查询,直到您获得 10 条或任意多条您想要的记录。在某些时候删除数字将变得毫无意义,因为用户显然输入了错误的邮政编码。

于 2013-09-30T20:31:06.380 回答