简体   繁体   中英

Remove selected entry on other drop-down list

I have 2 DropDownList s with same contents (ie finance, marketing, promotion). I want to remove already-selected values from the rest of the list.

Example: If I select "finance" for the 1st list, it should be removed on other list; the 2nd list should only display "marketing" and "promotion".

However, the current code still display all values on other list when whatever value is selected on the 1st one.

ASP.NET Page

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

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

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

You need to maintain master collection which will hold dropdown list values and one collection which will hold selected values. Bind dropdown by applying filter as master collection - selected collection. And you need to add or delete from selected collection only if dropdown value changes.

So consider example as -

MasterList collection -finance, marketing ,promotion Selected Collection - Empty

Now for each dropdown you apply filter Masterlist - Selected and bind.

So for all Dropdown values marketing, Finance, Promotion. You select Marketing for first dropdown then for second dropdown values available ill be =Masterlist - Selected = Finance and Promition.

And if you are binding dropdown only by querying DB then it will be simple.

//i tried this and it worked

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM