如何基于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( DataField
和DataValue
是值)。
Label1.Text += "All Providers
"; Label1.Text += string.Join("
", distinctValues2); //Displays all providers related to the location
填充Name
下拉列表( DataField
和DataValue
是值)。
Label1.Text += "All Topics
"; Label1.Text += string.Join("
", distinctValues3); //Displays all topics related to the specialty of that location
填充Topic
下拉列表( DataField
和DataValue
是值)。
Specialty.DataSource = distinctValues Specialty.DataBind()
您可能需要调整DataTextField
和DataValueField
属性,但我不知道它们应该是什么。
对所有三个下拉列表执行相同操作。
我还将此代码划分为单独的函数。 也许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();
- 如何首先使用代码在已创建的数据库中创建ASP.net身份表?
- 如何将EF Code First DbContext绑定到Asp.Net DataSource?
- 使用图像控件从SQL数据库显示特定图像
- 如何在ASP.NET中开发的Azure Web App上获取客户端的IP地址?
- 将WSDL对象获取到SQL数据库中
- C#Alter Table并以编程方式添加一列ASP.Net和SQL Server
- 在运行时使用和调用SOAP WebServices – 来自WSDL文件的动态Web服务客户端
- ASP.NET WebApi IExceptionLogger不捕获exception
- 我应该永远不会调用HostingEnvironment.UnregisterObject吗?