如何根据另一个选择筛选一个下拉列表

本文关键字:一个 下拉列表 筛选 何根 另一个 选择 | 更新日期: 2023-09-27 18:34:25

我有以下代码填充Topic下拉列表并将其保存到缓存表中:

bookingData2 = new DataTable();
DataTable DTable_List = new DataTable();
string connString = @"";
string query2 = @"Select * from [DB].dbo.[top]";// columng #1 = Specialty and column #2 = Topic
using (SqlConnection conn = new SqlConnection(connString))
{
    try
    {
        SqlCommand cmd = new SqlCommand(query2, conn);
        SqlDataAdapter da = new SqlDataAdapter(query2, conn);
        da.Fill(bookingData2);
        HttpContext.Current.Cache["cachedtable2"] = bookingData2;
        bookingData2.DefaultView.Sort = "Topic ASC";
        Topic.DataSource = bookingData2.DefaultView.ToTable(true, "Topic"); // populate only with the Topic column
        Topic.DataTextField = "Topic";
        Topic.DataValueField = "Topic";
        Topic.DataBind();
        Topic.Items.Insert(0, new ListItem("All Topics", "All Topics"));
        da.Dispose();
    }
    catch (Exception ex)
    {
        string error = ex.Message;
    }
}

我有以下代码填充Specialty下拉列表并将其保存到另一个缓存表中:

bookingData = new DataTable();
DataTable DTable_List = new DataTable();
string connString = @"";
string query = @"select * from [DB].dbo.[SP]";
using (SqlConnection conn = new SqlConnection(connString))
{
    try
    {
        SqlCommand cmd = new SqlCommand(query, conn);
        SqlDataAdapter da = new SqlDataAdapter(query, conn);
        da.Fill(bookingData);
        bookingData.DefaultView.Sort = "Specialty ASC";
        Specialty.DataSource = bookingData.DefaultView.ToTable(true, "Specialty");
        Specialty.DataTextField = "Specialty";
        Specialty.DataValueField = "Specialty";
        Specialty.DataBind();
        Specialty.Items.Remove("All Specialties");
        Specialty.Items.Insert(0, new ListItem("All Specialties", "All Specialties"));
        da.Dispose();
    }
    catch (Exception ex)
    {
        string error = ex.Message;
    }
}

如何对Specialty下拉列表索引更改进行编码以执行以下操作并将其保存到缓存表中以便快速访问:

protected void Specialty_SelectedIndexChanged(object sender, EventArgs e)
{
    //re-populate the Topic dropdownlist to display all the topics based on the following criteria:
        --> Where the Specialty column is either "All Specialties" OR "{specialty selected index value}"
}

如何根据另一个选择筛选一个下拉列表

如果不是

太重,bookingData2表保存在ViewState或会话中(我不建议使用会话)。否则,最好缓存它或再次查询数据库以重新填充它。

假设您将bookingData2保存在ViewState中,如下所示Page_Load

ViewState["bookingData2"] = bookingData2; // This should be before the following line
Topic.DataSource = bookingData2.DefaultView.ToTable(true, "Topic");

然后在您的SelectedIndexChanged事件中做这样的事情

protected void Specialty_SelectedIndexChanged(object sender, EventArgs e)
{
    //re-populate the Topic dropdownlist to display all the topics based on the following criteria:
    // Where the Specialty column is either "All Specialties" OR "{specialty selected index value}"
    DataTable bookingData2 = (DataTable)ViewState["bookingData2"];
    Topic.DataSource = bookingData2.Where(i => i.Specialty == "All Specialties" || i.Specialty == Specialty.SelectedValue).DefaultView.ToTable(true, "Topic"); // populate only with the Topic column
    Topic.DataTextField = "Topic";
    Topic.DataValueField = "Topic";
    Topic.DataBind();
    Topic.Items.Insert(0, new ListItem("All Topics", "All Topics"));
}

更新 - 使用缓存对象

在事件中执行以下操作Specialty_SelectedIndexChanged而不是我们以前使用ViewState的位置。

if (HttpRuntime.Current.Cache["cachedtable2"] != null)
{
    DataTable bookingData2 = HttpRuntime.Current.Cache["cachedtable2"] as DataTable;
    // Rest of the code
}

我还没有尝试过这段代码。如果您发现任何问题,请告诉我。

这就是为我解决的问题:

protected void Topic_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        if (Topic.SelectedIndex == 0)
        {
            string query = @"Specialty LIKE '%%'";
            DataTable cacheTable = HttpContext.Current.Cache["cachedtable"] as DataTable;
            DataTable filteredData = cacheTable.Select(query).CopyToDataTable<DataRow>();
            filteredData.DefaultView.Sort = "Specialty ASC";
            Specialty.DataSource = filteredData.DefaultView.ToTable(true, "Specialty");
            Specialty.DataTextField = "Specialty";
            Specialty.DataValueField = "Specialty";
            Specialty.DataBind();
        }
        else
        {
            string qpopulate = @"[Topic] = '" + Topic.SelectedItem.Value + "' or [Topic] = 'All Topics'"; //@"Select * from [DB].dbo.[table2] where [Specialty] = '" + Specialty.SelectedItem.Value + "' or [Specialty] = 'All Specialties'";
            DataTable cTable = HttpContext.Current.Cache["cachedtable2"] as DataTable;
            DataTable fData = cTable.Select(qpopulate).CopyToDataTable<DataRow>();
            if (fData.Rows.Count > 0)
            {
                fData.DefaultView.Sort = "Specialty ASC";
                Specialty.DataSource = fData.DefaultView.ToTable(true, "Specialty");
                Specialty.DataTextField = "Specialty";
                Specialty.DataValueField = "Specialty";
                Specialty.DataBind();
            }
            Specialty.Items.Insert(0, new ListItem("All Specialties", "All Specialties"));
        }
    }
    catch (Exception ce)
    {
        string error = ce.Message;
    }
}