从ASP.NET中的DropDownList中选择后,填充多个文本框

时间:2013-12-03 17:13:54

标签: c# asp.net sql

我有一个链接到SQL数据库的DropDownList。它目前显示了一个客户列表。我试图这样做,以便一旦选择了客户,就会自动填充多个文本框(例如地址,城市等)。我能够自动填充“公司名称”文本框,因为该值是所选的值,但我不知道如何使用行中的其余数据填充其他文本框。我最好怎么做呢?

在.aspx中:

<asp:DropDownList ID="DropDownList1" runat="server" ></asp:DropDownList>

C#:

DataTable customers = new DataTable();
...

SqlDataAdapter adapter = new SqlDataAdapter("SELECT CustomerName FROM Customers.dbo.Customer", connection);
                adapter.Fill(customers);
                DropDownList1.DataSource = customers;
                DropDownList1.DataTextField = "CustomerName";
                DropDownList1.DataValueField = "CustomerName";
                DropDownList1.DataBind();

编辑:感谢Karl的帮助。如果您遇到类似问题,请听从他的建议。此外,请务必更改下拉列表,使其如下所示:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" onselectedindexchanged="CompanyChanged"></asp:DropDownList>

EDIT2:对于那些遇到同样问题的人..这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)

    {
        LoadOptions();
    }
}
protected void LoadOptions()
    {
        DataTable customers = new DataTable();

        SqlConnection connection = new SqlConnection(INSERT YOUR CONNECTION STRING HERE);
        using (connection)
        {
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT Column1 FROM Table", connection);

            adapter.Fill(customers);
            DropDownListID.DataSource = customers;

            companyselect.DataTextField = "Column1";
            companyselect.DataValueField = "Column1";
            companyselect.DataBind(); 
        }
    }

protected void SelectionChanged(object sender, EventArgs e)
    {
        string selected= DropDownListID.SelectedItem.Value;

        SqlConnection connection = new SqlConnection(YOUR CONNECTION STRING);
        using (connection)
        {

            SqlCommand command = new SqlCommand("SELECT * FROM Table WHERE Column1= @Column1", connection);
            command.Parameters.AddWithValue("@Column1", selected);
            command.CommandType = CommandType.Text;
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            using (reader)
            {
                if (reader.HasRows)
                {
                    reader.Read();

//add as many as needed to fill your textboxes
                    TextBox1.Text = reader.GetString(1);
                    TextBox2.Text = reader.GetString(2);
                    TextBox3.Text = reader.GetString(3);
                }
                else { }
            }
        }
    }

这就是我的下拉列表:

<asp:DropDownList  ID="DropDownListID" runat="server" AutoPostBack="true" onselectedindexchanged="SelectionChanged"></asp:DropDownList>

2 个答案:

答案 0 :(得分:3)

我强烈建议您使用数据库中的CustomerID或等效ID字段作为下拉列表中的DataValueField,因为您可能希望在数据库中允许相同的客户名称,但能够通过身份证明将他们分开。

相反,试试这个:

SqlDataAdapter adapter = new SqlDataAdapter("SELECT CustomerName, CustomerID FROM Customers.dbo.Customer", connection);
adapter.Fill(customers);
DropDownList1.DataSource = customers;
DropDownList1.DataTextField = "CustomerName";
DropDownList1.DataValueField = "CustomerID";
DropDownList1.DataBind();
  

注意:将CustomerID更改为数据库表中实际ID列名称的实际值。

现在,在您的下拉列表的SelectedIndexChanged事件中,获取所选项目的ID并查询数据库中的单行数据,如下所示:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    string selectedID = DropDownList1.SelectedItem.Value;

    SqlCommand theCommand = new SqlCommand("SELECT * FROM Customers.dbo.Customer WHERE CustomerID = @CustomerID", connection);
    theCommand.Paramters.AddWithValue("@CustomerID", selectedId);
    theCommand.CommandType = CommandType.Text;

    using (SqlDataReader theReader = theCommand.ExecuteReader())
    {
        if (theReader.HasRows)
        {
            // Get the first row
            theReader.Read();

            // Set the text box values
            CustomerName.Text = theReader.GetString(0);
            ...
        }
    } 
}
  

注意:检索单行客户数据使用参数化查询来避免SQL注入漏洞。此外,上面的代码使用SqlCommandSqlDataReader以及using块来正确处理读者对象。

答案 1 :(得分:0)

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            SQLconn.Open();

            string req = "select distinct CapaciteTotal from Avion where NomAvion=" + DropDownList1.Text + "";
            SqlCommand com = new SqlCommand(req, SQLconn);
            SqlDataReader dr = com.ExecuteReader();
            if (dr.Read())
            {
                TextBox1.Text = dr.GetValue(1).ToString();
                if (DropDownList4.Text == "Aller Retour")
                {
                    TextBox2.Text = dr.GetValue(1).ToString();
                }

            }
        }
        catch(Exception ex)
        {
            HttpContext.Current.Response.Write("<script>alert('Probleme de la base de donnee : " + ex.Message + "')</script>");
        }
        finally
        {
            SQLconn.Close();
        }

    }