如何基于DataTable填充下拉列表

时间:2015-01-21 19:40:48

标签: c# asp.net drop-down-menu

我有以下代码,它使用从SP和CachedTable检索的值填充ASP.net标签:

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<string>("Specialty"))
                   .Distinct();
            Label1.Text = "All Specialties<br/>";
            Label1.Text += string.Join("<br />", 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<DataRow>();

            var distinctValues2 = filteredData2.AsEnumerable()
                   .Select(row => row.Field<string>("Name"))
                   .Distinct();
            Label1.Text += "All Providers<br/>";
            Label1.Text += string.Join("<br />", 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<DataRow>();

            var distinctValues3 = filteredData3.AsEnumerable()
                   .Select(row => row.Field<string>("Topic"))
                   .Distinct();
            Label1.Text += "All Topics<br/>";
            Label1.Text += string.Join("<br />", distinctValues3); //Displays all topics related to the specialty of that location

            Session.Add("DTTableLocation", dt);
        }
    }
}

我有四个下拉列表:

<asp:DropDownList ID="Location" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Location_SelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList ID="Specialty" runat="server" AutoPostBack="true"></asp:DropDownList>
<asp:DropDownList ID="Name" runat="server" AutoPostBack="true"></asp:DropDownList>
<asp:DropDownList ID="Topic" runat="server" AutoPostBack="true"></asp:DropDownList>

上述C#代码是针对Location_SelectedIndexChanged执行的。

如何替换以下内容:

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

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

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

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

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

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

1 个答案:

答案 0 :(得分:0)

Specialty.DataSource = distinctValues
Specialty.DataBind()

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

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

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

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

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();
相关问题