0

我有 3 个下拉列表 (DDL),它们都绑定到名为policyID 的列。

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

  1. 123456
  2. 234567
  3. 345678

当数据已经在一个 ddl 中被选中时,我试图阻止数据出现在另一个 ddl 中。

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

  1. DDL1
  2. DDL2
  3. DDL3

如果 DDL1 选择 123456,这个值 123456 将不会出现在其他 2 个 DDL 中。

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

我在这个帖子上问了一个类似的问题

我用过更新面板

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


            }
4

2 回答 2

0

您可以使用缓存来实现这一点。像这样

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

对第三个下拉菜单使用相同的方法。

于 2013-08-02T06:20:22.020 回答
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();

希望这可以帮助

于 2013-08-02T07:09:07.547 回答