删除其他下拉列表中的选定条目
本文关键字:其他 下拉列表 删除 | 更新日期: 2023-09-27 18:36:47
我有 2 个内容相同的DropDownList
(即财务、营销、促销)。我想从列表的其余部分删除已选择的值。
示例:如果我为第一个列表选择"财务",则应在其他列表中将其删除;第二个列表应仅显示"营销"和"促销"。
但是,当在第一个列表中选择任何值时,当前代码仍显示其他列表中的所有值。
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();
}
}
在选定索引更改时
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();
}
}
您需要维护将保存下拉列表值的主集合和一个将保存选定值的集合。通过将筛选器应用为主集合 - 所选集合来绑定下拉列表。并且,仅当下拉值更改时,才需要从所选集合中添加或删除。
所以考虑例子是 -
主列表集合-财务,营销,推广选定的集合 - 空
现在,对于每个下拉列表,您可以应用过滤器主列表 - 已选择并绑定。
因此,对于所有下拉列表都重视营销,财务,促销。为第一个下拉列表选择"市场营销",然后为第二个下拉列表值选择"可用",条件为 =主列表 - 已选择=财务和推广。
如果您仅通过查询数据库来绑定下拉列表,那么这将很简单。
//我试过这个,它有效
<script type="text/javascript">
function funcCheck(obj)
{
var isSelected=false;
var selectedVal=$(obj).val();
var ctrlId=$(obj).attr("id");
alert(selectedVal);
alert($("[id*=ddlist]").length);
$("[id*=ddlist]").each(function(){
if($.trim($(this).attr("id"))!=$.trim(ctrlId))
{
if(selectedVal == $(this).val())
{
isSelected=true;
return;
}
}
});
if(isSelected)
{
alert("Value is already selected");
$(obj).val(0);
return false;
}
}
</script>