如何根据其他选择过滤一个下拉列表

时间:2014-12-15 21:43:41

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

我有以下代码填充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}"
}

2 个答案:

答案 0 :(得分:1)

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

我们假设您在Page_Load

中按如下方式在viewState中保存bookingData2
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
}

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

答案 1 :(得分:1)

这就是为我解决的问题:

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;
    }
}