我有2 DropDownList
s具有相同的内容(即金融,市场营销,促销)。我想从列表的其余部分删除已选定的值。删除其他下拉列表中的选定条目
示例:如果我为第一个列表选择“finance”,则应该在其他列表中删除它;第二个列表应该只显示“营销”和“促销”。
但是,当第一个值选择任何值时,当前的代码仍然在其他列表中显示所有值。
ASP.NET页面
<asp:DataList ID="dldepart" runat="server" RepeatDirection="Horizontal" RepeatColumns="4" Height="343px" Width="1572px" onitemdatabound="dldepart_ItemDataBound">
<ItemTemplate>
<asp:DropDownList ID="ddlist" runat="server" AutoPostBack="true" onselectedindexchanged="ddlist_SelectedIndexChanged">
</asp:DropDownList>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
</ItemTemplate>
</asp:DataList>
ASP.NET C#代码
private void BindCheckBoxList()
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\database\personal.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
try
{
con.Open();
SqlCommand Cmd = new SqlCommand("SELECT distinct depart FROM datalist", con);
SqlDataAdapter Da = new SqlDataAdapter(Cmd);
Da.Fill(dt);
if (dt.Rows.Count > 0)
{
dldepart.DataSource = dt;
dldepart.DataBind();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
protected void dldepart_ItemDataBound(object sender, DataListItemEventArgs e)
{
DropDownList ddlist = (DropDownList)e.Item.FindControl("ddlist");
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\database\personal.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
try
{
con.Open();
SqlCommand Cmd = new SqlCommand("SELECT distinct depart FROM datalist", con);
SqlDataAdapter Da = new SqlDataAdapter(Cmd);
/**codes that i used to repeat datalist **/
Da.Fill(dt);
if (dt.Rows.Count > 0)
{
ddlist.DataSource=dt;
ddlist.DataTextField="depart";
ddlist.DataValueField="depart";
ddlist.DataBind();
ddlist.Items.Insert(0, "Select");
ddlist.Items.FindByText("Select").Value = Convert.ToString(0);
}
}
catch (System.Data.SqlClient.SqlException ex)
{
}
finally
{
con.Close();
}
}
在SelectedIndexChange
protected void ddlist_SelectedIndexChanged(object sender, EventArgs e)
{
DataListItem dlitem = (DataListItem)((DropDownList)sender).Parent;
CheckBoxList CheckBoxList1 = (CheckBoxList)dlitem.FindControl("CheckBoxList1");
DropDownList ddlist = (DropDownList)dlitem.FindControl("ddlist");
// DataBoundControl DataSource = (DataBind)dldepart.FindControl("DataSource");
DataSet ds = new DataSet();
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\database\personal.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
try
{
con.Open();
SqlCommand Cmd = new SqlCommand("SELECT Id,subDepatment FROM datalist where depart='" + ddlist.SelectedItem.Text + "'", con);
SqlDataAdapter Da = new SqlDataAdapter(Cmd);
Da.Fill(dt);
if (dt.Rows.Count > 0)
{
CheckBoxList1.DataSource = dt;
CheckBoxList1.DataTextField = "subDepatment"; // the items to be displayed in the list items
CheckBoxList1.DataValueField = "Id"; // the id of the items displayed
CheckBoxList1.DataBind();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
}
finally
{
con.Close();
}
}