下拉列表中没有重复的数据值

本文关键字:数据 下拉列表 | 更新日期: 2023-09-27 18:25:55

如何停止DropDownLists中数据的重复?我找不到任何具有sql中不重复值的DropDownlists的好例子?我试着让select语句说Distinct。

这是我正在使用的代码:

protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = (string)Session["name"];
    if (!IsPostBack)
    {
        DropDownList1.AppendDataBoundItems = true;
        DropDownList2.AppendDataBoundItems = true;
        DropDownList3.AppendDataBoundItems = true;
        DropDownList4.AppendDataBoundItems = true;
        String strConnString = ConfigurationManager
            .ConnectionStrings["connectionString"].ConnectionString;
        String strQuery = "select distinct * from dbo.Vehiclemain WHERE ISENABLED = 'YES'";
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQuery;
        cmd.Connection = con;
        try
        {
            con.Open();
            DropDownList1.DataSource = cmd.ExecuteReader();
            DropDownList1.DataTextField = "Year";
            DropDownList1.DataValueField = "ID";
            DropDownList1.DataBind();
            con.Close();
            con.Open();
            DropDownList2.DataSource = cmd.ExecuteReader();
            DropDownList2.DataTextField = "Make";
            DropDownList2.DataValueField = "ID";
            DropDownList2.DataBind();
            con.Close();
            con.Open();
            DropDownList3.DataSource = cmd.ExecuteReader();
            DropDownList3.DataTextField = "Model";
            DropDownList3.DataValueField = "ID";
            DropDownList3.DataBind();
            con.Close();
            con.Open();
            DropDownList4.DataSource = cmd.ExecuteReader();
            DropDownList4.DataTextField = "Submodel";
            DropDownList4.DataValueField = "ID";
            DropDownList4.DataBind();
            con.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
    }

下拉列表中没有重复的数据值

看起来您想要在第一个下拉列表的基础上更改下拉列表。如果是这样的话,你会想让下拉列表也成为一个自动宿主。每次有人更改年份时,都会根据年份填写一个make。每次有人更改品牌时,都会根据品牌填充一个模型。每次有人更改模型时,都会根据模型填充一个子模型。sQL查询将在我们构建信息时被搭建起来。。。

以下代码尚未经过测试。

<asp:DropDownList ID="ddlYearObj" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlObj_SelectedIndexChanged" ></asp:DropDownList>
<asp:DropDownList ID="ddlMakeObj" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlMakeObj_SelectedIndexChanged" Visible="false"></asp:DropDownList>
<asp:DropDownList ID="ddlModelObj" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlModelObj_SelectedIndexChanged" Visible="false"></asp:DropDownList>
<asp:DropDownList ID="ddlSubmodelObj" runat="server" Visible="false"></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = (string)Session["name"];
    if (!IsPostBack)
    {
        String strConnString = ConfigurationManager
            .ConnectionStrings["connectionString"].ConnectionString;
        String strQuery = "select distinct Year from dbo.Vehiclemain WHERE ISENABLED = 'YES'";
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQuery;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlYearObj.DataSource = cmd.ExecuteReader();
            ddlYearObj.DataTextField = "Year";
            ddlYearObj.DataValueField = "Year";
            ddlYearObj.DataBind();
            con.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
        //Visible!? -- All invisible until something is chosen
        ddlMakeObj.Visible = false;
        ddlModelObj.Visible = false;
        ddlSubmodelObj.Visible = false;
    }
    protected void ddlYearObj_SelectedIndexChanged(object sender, EventArgs e)
    {
        String strConnString = ConfigurationManager
            .ConnectionStrings["connectionString"].ConnectionString;
        String strQuery = "select distinct Make from dbo.Vehiclemain WHERE ISENABLED = 'YES' AND YEAR = "+ ddlYearObj.Value;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQuery;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlMakeObj.DataSource = cmd.ExecuteReader();
            ddlMakeObj.DataTextField = "Make";
            ddlMakeObj.DataValueField = "Make";
            ddlMakeObj.DataBind();
            con.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
        //Visible!
        ddlMakeObj.Visible = true;
        ddlModelObj.Visible = false;
        ddlSubmodelObj.Visible = false;
    }
    protected void ddlMakeObj_SelectedIndexChanged(object sender, EventArgs e)
    {
        String strConnString = ConfigurationManager
            .ConnectionStrings["connectionString"].ConnectionString;
        String strQuery = "select distinct modelfrom dbo.Vehiclemain WHERE ISENABLED = 'YES' AND YEAR = "+ ddlYearObj.Value;
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQuery;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlModelObj.DataSource = cmd.ExecuteReader();
            ddlModelObj.DataTextField = "Model";
            ddlModelObj.DataValueField = "Model";
            ddlModelObj.DataBind();
            con.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
        //Visible!
        ddlMakeObj.Visible = true;
        ddlModelObj.Visible = true;
        ddlSubmodelObj.Visible = false;
    }
    protected void ddlModelObj_SelectedIndexChanged(object sender, EventArgs e)
    {
        String strConnString = ConfigurationManager
            .ConnectionStrings["connectionString"].ConnectionString;
        String strQuery = "select distinct Submodelfrom dbo.Vehiclemain WHERE ISENABLED = 'YES' AND YEAR = "+ ddlYearObj.Value + " AND Model = '" + ddlModelObj.Value + "'";
        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = strQuery;
        cmd.Connection = con;
        try
        {
            con.Open();
            ddlSubmodelObj.DataSource = cmd.ExecuteReader();
            ddlSubmodelObj.DataTextField = "Submodel";
            ddlSubmodelObj.DataValueField = "ID";
            ddlSubmodelObj.DataBind();
            con.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Dispose();
        }
        //Visible!
        ddlMakeObj.Visible = true;
        ddlModelObj.Visible = false;
        ddlSubmodelObj.Visible = true;
    }

您使用单个查询来填充所有下拉列表。尝试创建4个select语句,并将distinct应用于相应的列。for ex Make下拉列表将在查询中具有不同的Make。