简体   繁体   English

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

[英]Remove selected entry on other drop-down list

I have 2 DropDownList s with same contents (ie finance, marketing, promotion). 我有2个DropDownList具有相同的内容(即财务,市场营销,促销)。 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.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# Code 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();
    }
}

On SelectedIndexChange 在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 MasterList集合-财务,市场,促销选定的集合-空

Now for each dropdown you apply filter Masterlist - Selected and bind. 现在,为每个下拉列表应用过滤器“主列表-已选择并绑定”。

So for all Dropdown values marketing, Finance, Promotion. 因此,对于所有Dropdown价值营销,财务,促销。 You select Marketing for first dropdown then for second dropdown values available ill be =Masterlist - Selected = Finance and Promition. 您选择“市场营销”作为第一个下拉菜单,然后选择“第二个下拉菜单”值,除非= Masterlist-Selected = Finance and Promition。

And if you are binding dropdown only by querying DB then it will be simple. 而且,如果仅通过查询DB绑定下拉列表,那么它将很简单。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 angularjs根据其他下拉列表中的选定值过滤下拉列表 - angularjs filter drop-down list according to selected values in other drop-down lists 根据其他列表中选择的内容填充Telerik下拉列表 - Populate Telerik Drop-down list based on what is selected in other list 从下拉列表中获取所选文本,但使用jQuery删除数值 - Get selected text from a drop-down list but remove numeric value using jQuery 如何使用取决于另一个下拉列表中所选选项的下拉列表显示表格简码 - how to display table shortcode with a drop-down list that depends on the selected option in another drop-down list 根据另一个下拉列表中的所选选项更改下拉列表中的项目 - Change items in a drop-down list depending on the selected option in another drop-down list 根据另一个选定的下拉列表更改下拉列表中的选项 - Change options in a drop-down list depending on another selected drop-down list 从下拉列表中选择“其他”时动态要求文本框 - Dynamically require textbox when “Other” selected from a drop-down 从下拉可用选项中删除选定的选项 - remove selected option from drop-down available options 如何从其他下拉列表中删除在上一个下拉列表中选择的选项 - How to remove the option which was selected in previous drop down list from other drop down lists 在下拉列表中显示从月份选择中选择的图表列表 - Displaying a list of charts selected from a month selection in drop-down
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM