当另一个下拉列表选中重复数据时,防止重复数据出现在另一个下拉列表中

时间:2013-08-02 06:05:55

标签: c# asp.net drop-down-menu

我有3个下拉列表(DDL),所有这些都绑定到名为policeID的列。

policeID列有3个数据值,称为:

  1. 123456
  2. 234567
  3. 345678
  4. 我正在尝试阻止数据在另一个ddl中被选中时已出现在另一个ddl中。

    例如,我有3个DDL,它们是

    1. DDL1
    2. DDL2
    3. DDL3
    4. 如果DDL1选择123456,则该值123456不会出现在另外2个DDL中。

      但是,当用户取消选择123456并选择默认值时,此值123456将能够在所有3个DDL中看到。

      我在thread

      问了一个类似的问题

      我使用过更新面板

      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
      <ContentTemplate>
      </ContentTemplate>
      </asp:UpdatePanel>
      

      但它不起作用。有没有其他方法可以防止出现重复数据?

      的问候。

      这就是我将DDL绑定到SQL的方式。

      protected void ddllocation_SelectedIndexChanged(object sender, EventArgs e)
          {
      
              using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI"))
              {
                  connAdd.Open();
      
                  var sql = "Select policeid from PoliceAccount where status ='available' and handle ='offcase' and postedto='" + ddllocation.SelectedValue + "'";
                  using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
                  {
                      DataSet ds2 = new DataSet();
                      cmdAdd.Fill(ds2);
      
                      ddlpid1.Items.Clear();
                      ddlpid1.DataSource = ds2;
                      ddlpid1.DataTextField = "policeid";
                      ddlpid1.DataValueField = "policeid";
                      ddlpid1.DataBind();
                      ddlpid1.Items.Insert(0, new ListItem("Police ID", ""));
                      ddlpid1.SelectedIndex = 0;
      
                      ddlpid2.Items.Clear();
                      ddlpid2.DataSource = ds2;
                      ddlpid2.DataTextField = "policeid";
                      ddlpid2.DataValueField = "policeid";
                      ddlpid2.DataBind();
                      ddlpid2.Items.Insert(0, new ListItem("Police ID", ""));
                      ddlpid2.SelectedIndex = 0;
      
                      ddlpid3.Items.Clear();
                      ddlpid3.DataSource = ds2;
                      ddlpid3.DataTextField = "policeid";
                      ddlpid3.DataValueField = "policeid";
                      ddlpid3.DataBind();
                      ddlpid3.Items.Insert(0, new ListItem("Police ID", ""));
                      ddlpid3.SelectedIndex = 0;
      
                      ddlpid4.Items.Clear();
                      ddlpid4.DataSource = ds2;
                      ddlpid4.DataTextField = "policeid";
                      ddlpid4.DataValueField = "policeid";
                      ddlpid4.DataBind();
                      ddlpid4.Items.Insert(0, new ListItem("Police ID", ""));
                      ddlpid4.SelectedIndex = 0;
      
                      ddlpid5.Items.Clear();
                      ddlpid5.DataSource = ds2;
                      ddlpid5.DataTextField = "policeid";
                      ddlpid5.DataValueField = "policeid";
                      ddlpid5.DataBind();
                      ddlpid5.Items.Insert(0, new ListItem("Police ID", ""));
                      ddlpid5.SelectedIndex = 0;
      
      
                  }
      

2 个答案:

答案 0 :(得分:0)

您可以使用Cache来实现此目的。就像这个

    protected void bindDdls()
    {
        DataTable dt = new DataTable();
        try
        {
            ddlpid1.Items.Clear();
            ddlpid2.Items.Clear();
            using (var connAdd = new SqlConnection("Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI"))
    {
        connAdd.Open();

        var sql = "Select policeid from PoliceAccount where status ='available' and handle ='offcase' and postedto='" + ddllocation.SelectedValue + "'";
        using (var cmdAdd = new SqlDataAdapter(sql, connAdd))
        {
            DataSet ds2 = new DataSet();
            cmdAdd.Fill(ds2);
            dt = ds.Tables[0];

            if (dt.Rows.Count > 0)
            {
                ddlpid1.DataSource = dt;
                ddlpid1.DataTextField = dt.Columns["policeid"].ToString();
                ddlpid1.DataValueField = dt.Columns["policeid"].ToString();
                ddlpid1.DataBind();
                ddlpid1.Items.Insert(0, new ListItem("Select", "Select"));
            }
            else
            {
                ddlpid1.Items.Clear();
                ddlpid1.DataSource = null;
                ddlpid1.DataBind();
                ddlpid1.Items.Insert(0, new ListItem("Select", "Select"));
            }

            if (Cache["PoliceData"] == null)
            {
                Cache.Insert("PoliceData", dt);
            }
            else
            {
                Cache.Remove("PoliceData");
                Cache.Insert("PoliceData", dt);
            }

            connAdd.Close();
            }
          }
        }
        catch (Exception Ex)
        {
            throw Ex;
        }
        finally
        {
            if (connAdd.State == connAdd.Open)
                connAdd.Close();
        }
    }

    protected void ddlpid1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlpid1.SelectedValue != "Select")
        {
            bindSecondDropdown();
        }
    }

    protected void bindSecondDropdown()
    {
        DataTable dt = new DataTable();
        DataTable dtFiltered = new DataTable();
        try
        {
            ddlpid2.Items.Clear();
            if (Cache["PoliceData"] != null)
            {
                dt = (DataTable)Cache["PoliceData"];
                if (ddlpid1.SelectedValue != "Select" && !string.IsNullOrEmpty(ddlpid1.SelectedValue))
                    dt.DefaultView.RowFilter = "[policeID] <> " + ddlpid1.SelectedValue;
                else
                    dt.DefaultView.RowFilter = "[policeID] <> " + 0;
                dtFiltered = dt.DefaultView.ToTable();
            }

            if (dt.Rows.Count > 0)
            {
                ddlpid2.DataSource = dtFiltered;
                ddlpid2.DataTextField = dtFiltered.Columns["policeid"].ToString();
                ddlpid2.DataValueField = dtFiltered.Columns["policeid"].ToString();
                ddlpid2.DataBind();
                ddlpid2.Items.Insert(0, new ListItem("Select", "Select"));
            }
            else
            {
                ddlpid2.Items.Clear();
                ddlpid2.DataSource = null;
                ddlpid2.DataBind();
                ddlpid2.Items.Insert(0, new ListItem("Select", "Select"));
            }
        }
        catch (Exception Ex)
        {
            throw Ex;
        }
    }

使用相同的方法进行第三次下拉列表。

答案 1 :(得分:0)

您必须在SelectedIndexChanged事件上重新绑定DDL2和DDL3。

这是在SelectedIndexChanged

上重新绑定的伪代码
Dictionary<string, string> list = new Dictionary<string, string>();
for each item in datasource
{
list.Add(item.key, item.value);
}

if (ddl1.SelectedValue != defaultValue){

   var itemToRemove = list.First(kvp => kvp.Value == ddl1.SelectedValue);

   dic.Remove(itemToRemove .Key);
}

ddl2.DataSource = list;
ddl2.DataTextField = "Value";
ddl2.DataValueField = "Key";
ddl2.DataBind();

希望这有帮助