删除其他下拉列表中的选定条目

时间:2015-01-08 10:06:23

标签: javascript c# jquery asp.net datalist

我有2 DropDownList个相同的内容(即财务,营销,推广)。我想从列表的其余部分中删除已经选择的值。

示例:如果我选择" finance"对于第一个列表,它应该在其他列表中删除;第二个列表应该只显示" marketing"和"促销"。

但是,当在第一个上选择任何值时,当前代码仍会显示其他列表中的所有值。

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

2 个答案:

答案 0 :(得分:0)

您需要维护主集合,该集合将包含下拉列表值和一个将保存所选值的集合。通过将过滤器应用为主集合 - 选定集合来绑定下拉列表。只有当下拉值发生变化时,您才需要在所选集合中添加或删除。

因此,请将示例视为 -

MasterList集合 - 财务,营销,推广 选定的集合 - 空

现在,对于每个下拉列表,您都应用过滤器Masterlist - Selected和bind。

因此,所有Dropdown价值营销,财务,促销。 您选择市场营销作为第一个下拉列表,然后选择第二个下拉值,可以是= Masterlist - Selected = Finance and Promition。

如果你只是通过查询DB来绑定下拉列表,那么它将很简单。

答案 1 :(得分:0)

//我试过这个并且有效

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