如何基于另一个选择过滤一个的DropDownList [英] How to filter one dropdownlist based on another selection
问题描述
我有以下的code其中填充主题
的DropDownList并将其保存到缓存表:
bookingData2 =新的DataTable();
数据表DTable_List =新的DataTable();
字符串CONNSTRING = @;
字符串QUERY2 = @SELECT * FROM [DB] .dbo [顶]。; // columng#1 =专业和列#2 =主题
使用(SqlConnection的康恩=新的SqlConnection(CONNSTRING))
{
尝试
{
CMD的SqlCommand =新的SqlCommand(QUERY2,康涅狄格州);
SqlDataAdapter的大=新SqlDataAdapter的(QUERY2,康涅狄格州);
da.Fill(bookingData2); HttpContext.Current.Cache [cachedtable2] = bookingData2; bookingData2.DefaultView.Sort =主题ASC; Topic.DataSource = bookingData2.DefaultView.ToTable(真正的主题); //仅在主题栏填入
Topic.DataTextField =主题
Topic.DataValueField =主题
Topic.DataBind();
Topic.Items.Insert(0,新的ListItem(所有主题,所有主题)); da.Dispose();
}
赶上(异常前)
{
字符串错误= ex.Message;
}
}
我有以下的code其中填充特殊
DropDownList中,并将其保存到另一个缓存表:
bookingData =新的DataTable();
数据表DTable_List =新的DataTable();
字符串CONNSTRING = @;
查询字符串= @SELECT * FROM [DB] .dbo [SP]。使用(SqlConnection的康恩=新的SqlConnection(CONNSTRING))
{
尝试
{
CMD的SqlCommand =新的SqlCommand(查询,康涅狄格州);
SqlDataAdapter的大=新SqlDataAdapter的(查询,康涅狄格州);
da.Fill(bookingData); bookingData.DefaultView.Sort =专业ASC; Specialty.DataSource = bookingData.DefaultView.ToTable(真正的专业);
Specialty.DataTextField =特色;
Specialty.DataValueField =特色;
Specialty.DataBind();
Specialty.Items.Remove(所有特产);
Specialty.Items.Insert(0,新的ListItem(所有特产,所有特产)); da.Dispose();
}
赶上(异常前)
{
字符串错误= ex.Message;
}
}
我如何code中的特殊
DropDownList的指数变化做以下,并将其保存为快速访问缓存表:
保护无效Specialty_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
//重新填充DropDownList的主题来显示所有基于以下标准的主题:
- >凡专业列为所有特产或{专业选择的索引值}
}
保存 bookingData2
表中的ViewState
或会话(我不会推荐使用虽然会话),如果它不是太沉重。否则,它的更好您缓存,或再次查询数据库重新填充它。
让我们假设你保存bookingData2在ViewState中为的Page_Load
的ViewState [bookingData2] = bookingData2; //这应该是以下行前
Topic.DataSource = bookingData2.DefaultView.ToTable(真正的主题);
然后在你的的SelectedIndexChanged
事件做这样的事情。
保护无效Specialty_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
//重新填充DropDownList的主题来显示所有基于以下标准的主题:
//其中专业列为所有特产或{专业选择的索引值}
数据表bookingData2 =(数据表)的ViewState [bookingData2]; Topic.DataSource = bookingData2.Where(I => i.Specialty ==所有特产|| i.Specialty == Specialty.SelectedValue).DefaultView.ToTable(真正的主题); //仅在主题栏填入
Topic.DataTextField =主题
Topic.DataValueField =主题
Topic.DataBind();
Topic.Items.Insert(0,新的ListItem(所有主题,所有主题));}
更新 - 随着缓存的对象
请按照 Specialty_SelectedIndexChanged
事件,而不是我们用在的ViewState
之前。
如果(HttpRuntime.Current.Cache [cachedtable2]!= NULL)
{
数据表bookingData2 = HttpRuntime.Current.Cache [cachedtable2]作为数据表;
在code的//休息
}
我还没有试过这种code。我们如果发现任何问题,我知道了。
I have the following code which populates the Topic
dropdownlist and saves it to a cached table:
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;
}
}
I have the following code which populates the Specialty
dropdownlist and saves it to another cached table:
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;
}
}
How can I code the Specialty
dropdownlist index change to do the following and save it to a cache table for quick access:
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}"
}
Save bookingData2
table in ViewState
or Session (I won't recommend to use session though) if it's not too heavy. Otherwise, its better you cache it or query the database again to repopulate it.
Let's assume you save bookingData2 in ViewState as follows in Page_Load
ViewState["bookingData2"] = bookingData2; // This should be before the following line
Topic.DataSource = bookingData2.DefaultView.ToTable(true, "Topic");
Then in your SelectedIndexChanged
event do something like this
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"));
}
Update - With Cached object
Do following in Specialty_SelectedIndexChanged
event instead of where we used ViewState
before.
if (HttpRuntime.Current.Cache["cachedtable2"] != null)
{
DataTable bookingData2 = HttpRuntime.Current.Cache["cachedtable2"] as DataTable;
// Rest of the code
}
I haven't tried this code. Let me know if you find any issues.
这篇关于如何基于另一个选择过滤一个的DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!