【问题标题】:Fill multiple textboxes after selecting from a DropDownList in ASP.NET从 ASP.NET 中的 DropDownList 中选择后填充多个文本框
【发布时间】:2013-12-03 17:13:53
【问题描述】:

我有一个链接到 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();

编辑:感谢卡尔的帮助。如果您有类似的问题,请遵循他的建议。另外,请确保更改下拉列表,使其看起来像:

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

【问题讨论】:

  • 发布您的代码是很好的第一步。
  • @KarlAnderson 我添加了我的代码。希望这会有所帮助。
  • 恕我直言,最好的方法是像您正在做的那样对数据库进行一次调用。一旦选择了一个。再打一个电话以获取详细信息。这样,如果您有 1000 个客户,您就不会拉下 10,000 行(10 个字段)
  • 数据库中有标识列吗?
  • @logixologist 是的,数据库中的每个客户都有一个 ID。选择选项后,如何再次调用数据库以获取其余详细信息?

标签: c# asp.net sql


【解决方案1】:

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

试试这个:

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 块来正确处理读取器对象。

【讨论】:

  • 这似乎有点工作,但返回的 selectedID 始终为 0。一旦选择了一个选项,带有 CustomerID 的文本框将显示 0 并且 DropDownList 重置。知道为什么会这样吗?
  • 发现问题!每次选择一个选项时,我都会刷新 ddl 的选项。它应该看起来像 Page_Load(..){ if(!IsPostBack) {LoadOptions();} }。感谢您的帮助!
  • @teabos - 没问题,很高兴它对你有用。如有可能,请随意对答案进行投票。 :-)
  • 谢谢卡尔,很好的回答,所以我为你 +1 了。我不得不参加一个会议,所以我无法回答他的问题;)
【解决方案2】:
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();
        }

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多