"全选"两个或多个下拉列表的选项

时间:2014-09-08 06:34:47

标签: c# drop-down-menu selectall

我有两个下拉列表,其中一个是" State"另一个是#34; Cluster"。这两个下拉列表都是关系型的。例如,如果我在州选择喀拉拉邦,那么群集将填充其重要城市和dist。像Pathnamthitta,calicut,palakad等。

所以,我想要的是,我想给出一个选项" Select All"在州和群ddl。

我经历过:

How to select all records in a DropDownList

http://www.w3schools.com/aspnet/control_dropdownlist.asp

http://forums.asp.net/t/1221977.aspx?Adding+a+Select+all+option+to+a+drop+down+list

但他们都没有为我工作。我可以选择所有状态,但不能在其他ddl上填充聚类值。

这是我的代码" State" DDL:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet3TableAdapters.tbl_energy_reportTableAdapter state;
state = new DataSet3TableAdapters.tbl_energy_reportTableAdapter();
DataTable dt = new DataTable();
dt = state.GetDataByStateInnerJoin();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "state1";
DropDownList1.DataValueField = "state1";
DropDownList1.DataBind();                
DropDownList1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--Select State--", "0"));
DropDownList2.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--Select Cluster--", "0"));
}
}

此代码适用于" Cluster"

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

DataSet3TableAdapters.tbl_energy_reportTableAdapter state;
state = new DataSet3TableAdapters.tbl_energy_reportTableAdapter();
DataTable dt = new DataTable();
dt = state.GetDataByClusterInnerJoin(DropDownList1.SelectedValue);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "cluster";
DropDownList2.DataValueField = "cluster";
DropDownList2.DataBind();

}

这是State ddl:

的查询
SELECT DISTINCT tbl_site_details.state1
FROM            tbl_site_details INNER JOIN
                         tbl_energy_report ON tbl_energy_report.Site_ID = tbl_site_details.site_id

这是Cluster ddl:

的查询
SELECT        tbl_site_details.cluster
FROM            tbl_site_details INNER JOIN
                         tbl_energy_report ON tbl_energy_report.Site_ID = tbl_site_details.site_id
WHERE        (tbl_site_details.state1 = @state1)

2 个答案:

答案 0 :(得分:0)

那是因为在ddl1_selectedindexchanged方法上使用dt重新绑定ddl2。

评论' DropDownList2.Items.Insert'在你的page_load上

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataSet3TableAdapters.tbl_energy_reportTableAdapter state;
state = new DataSet3TableAdapters.tbl_energy_reportTableAdapter();
DataTable dt = new DataTable();
dt = state.GetDataByStateInnerJoin();
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "state1";
DropDownList1.DataValueField = "state1";
DropDownList1.DataBind();                
DropDownList1.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--Select State--", "0"));
//COMMENT THIS LINE
//DropDownList2.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--Select Cluster--", "0"));
}
}

并将其移至dropdownlist1_selectedindexchanged事件。

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

DataSet3TableAdapters.tbl_energy_reportTableAdapter state;
state = new DataSet3TableAdapters.tbl_energy_reportTableAdapter();
DataTable dt = new DataTable();
dt = state.GetDataByClusterInnerJoin(DropDownList1.SelectedValue);
DropDownList2.DataSource = dt;
DropDownList2.DataTextField = "cluster";
DropDownList2.DataValueField = "cluster";
DropDownList2.DataBind();

//put it here
DropDownList2.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--Select Cluster--", "0"));
}

答案 1 :(得分:0)

最好使用Ajax调用来填充第二个下拉列表。使用包含选定ID的搜索字符串向服务器发送请求,并使用结果填充第二个下拉列表。将结果作为json对象返回。