如何基于DataTable填充下拉列表

我有以下代码填充ASP.net标签,其中包含从SP和CachedTable检索的值:

public void UpdateDropDownList() { string strQuery = ""; using (SqlConnection scCon = new SqlConnection(connString)) { using (SqlCommand scCmd = new SqlCommand("cation", scCon)) { SqlDataAdapter sda = new SqlDataAdapter(); DataTable dt = new DataTable(); scCmd.CommandType = CommandType.StoredProcedure; scCmd.Parameters.Add("@ation", SqlDbType.VarChar).Value = cation.SelectedItem.Value; sda.SelectCommand = scCmd; sda.Fill(dt); var distinctValues = dt.AsEnumerable() .Select(row => row.Field("Specialty")) .Distinct(); Label1.Text = "All Specialties
"; Label1.Text += string.Join("
", distinctValues); //Displays all specialties related to the location var k = distinctValues.ToArray(); strQuery = "("; for (int y = 0; y < k.Length; y++) { if (y == 0) { strQuery += @"[Specialty] = '" + k[y] + "'"; } else { strQuery += @" OR [Specialty] = '" + k[y] + "'"; } } strQuery += @") AND ([Location] = '" + Location.SelectedItem.Value + "')"; DataTable cacheTable2 = HttpContext.Current.Cache["cachedtable"] as DataTable; //first cached table DataTable filteredData2 = cacheTable2.Select(strQuery).CopyToDataTable(); var distinctValues2 = filteredData2.AsEnumerable() .Select(row => row.Field("Name")) .Distinct(); Label1.Text += "All Providers
"; Label1.Text += string.Join("
", distinctValues2); //Displays all providers related to the location strQuery = "([Specialty] = 'All Specialties'"; for (int y = 0; y < k.Length; y++) { strQuery += @" OR [Specialty] = '" + k[y] + "'"; } strQuery += ")"; DataTable cacheTable3 = HttpContext.Current.Cache["cachedtable2"] as DataTable; //different cached table DataTable filteredData3 = cacheTable3.Select(strQuery).CopyToDataTable(); var distinctValues3 = filteredData3.AsEnumerable() .Select(row => row.Field("Topic")) .Distinct(); Label1.Text += "All Topics
"; Label1.Text += string.Join("
", distinctValues3); //Displays all topics related to the specialty of that location Session.Add("DTTableLocation", dt); } } }

我有四个下拉列表:

     

上面的C#代码是为Location_SelectedIndexChanged执行的。

如何更换以下内容:

 Label1.Text = "All Specialties
"; Label1.Text += string.Join("
", distinctValues); //Displays all specialties related to the location

填充Specialty dropdownlist( DataFieldDataValue是值)。

 Label1.Text += "All Providers
"; Label1.Text += string.Join("
", distinctValues2); //Displays all providers related to the location

填充Name下拉列表( DataFieldDataValue是值)。

 Label1.Text += "All Topics
"; Label1.Text += string.Join("
", distinctValues3); //Displays all topics related to the specialty of that location

填充Topic下拉列表( DataFieldDataValue是值)。

 Specialty.DataSource = distinctValues Specialty.DataBind() 

您可能需要调整DataTextFieldDataValueField属性,但我不知道它们应该是什么。

对所有三个下拉列表执行相同操作。

我还将此代码划分为单独的函数。 也许GetSpecialties从数据库中检索所有专业。 GetProviders构建查询并从缓存表返回提供程序。 和GetTopics构建该查询并返回主题。

这将使主要function更短,更容易理解。 因为它现在很难阅读。

 var distinctSpecialties = GetSpecialties(); Specialty.DataSource = distinctSpecialties; Specialty.DataBind(); var distinctProviders = GetProviders(distinctSpecialties); Name.DataSource = distinctProviders; Name.DataBind(); var distinctTopics = GetTopics(distinctSpecialties); Topic.DataSource = distinctTopics; Topic.DataBind();