在ASP.NET中选择两列DropDownList后,填充多个文本框

本文关键字:DropDownList 填充 文本 两列 NET ASP 选择 | 更新日期: 2023-09-27 18:24:09

我有一个DropDownList,它包含两列,一列是CardCode,另一列是CardName,链接到SQL数据库。它目前显示的是CardCode列表+Cardname列表。我正在努力做到,一旦从两列下拉列表中选择了CardCode+CardName,就会自动填充多个文本框(如CardNum、CntctPerson、ListNum等)。我现在只选择CardCode就可以自动填充数据了。我想将相关行显示到CardCode+CardName下拉列表中,我不知道如何通过选择2列的下拉列表(CardCode+Card Name)来填充其他行。我该怎么做?提前感谢

这是下面的aspx.cs代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace StackOver
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadOptions();
            }
        }
        protected void LoadOptions()
        {
            DataTable CardCode = new DataTable();
            SqlConnection connection = new SqlConnection("my connection string");
            using (connection)
            {
                SqlDataAdapter adapter = new SqlDataAdapter("SELECT CardCode,CardName, Address, CntctPrsn FROM OCRD", connection);
                adapter.Fill(CardCode);
                DropDownList1.DataValueField = "CardCode";
                DropDownList1.DataTextField = "CardCode";
                DropDownList1.DataBind();
            }
        }
       protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {

            string selected = DropDownList1.SelectedItem.Value;
            SqlConnection connection = new SqlConnection("my connection string");
            using (connection)
            {
                SqlCommand theCommand = new SqlCommand("SELECT CardCode, CardName, Address, CntctPrsn FROM OCRD WHERE CardCode = @CardCode", connection);
                connection.Open();
                theCommand.Parameters.AddWithValue("@CardCode", selected);
                theCommand.CommandType = CommandType.Text;
                SqlDataReader theReader = theCommand.ExecuteReader();
                   if (theReader.Read())
                    {
                        // Get the first row
                      // theReader.Read();
                        // Set the text box values
                        this.TextBox1.Text = theReader.GetString(0);
                        this.TextBox2.Text = theReader.GetString(1);
                        this.TextBox3.Text = theReader.GetString(2);
                     //   this.TextBox3 = reader.IsDBNull(TextBox3Index) ? null : reader.GetInt32(TextBox3Index)
                        // GenreID = reader.IsDBNull(genreIDIndex) ? null : reader.GetInt32(genreIDIndex)
                        this.TextBox4.Text = theReader.GetString(3);
                        //  TextBox5.Text = theReader.GetString(4);
                        //  TextBox6.Text = theReader.GetString(5);
                        //  TextBox7.Text = theReader.GetString(6);
                    }
                    connection.Close();
                }
            }

       public object TextBox3Index { get; set; }
    }
}

这也是我的.aspx代码

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="StackOver._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        Welcome to ASP.NET!
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
            DataSourceID="SqlDataSource1" DataTextField="CardCode" 
            DataValueField="CardName" 
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:myconnectionstring %>" 
            SelectCommand="SELECT [CardCode] + '----' + [CardName] as CardCode, CardName,[Address], [CntctPrsn] FROM [OCRD]">
        </asp:SqlDataSource>
    </h2>
           <br />
        <br />
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
    Business Partner Code :&nbsp; 
        <asp:TextBox ID="TextBox1" runat="server" Width="192px" ></asp:TextBox>
    </p>
    <p>
    Business Partner Name :
        <asp:TextBox ID="TextBox2" runat="server" Width="192px" ></asp:TextBox>
    </p>
    <p>
     Address :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
        <asp:TextBox ID="TextBox3" runat="server" Width="196px" ></asp:TextBox>
    </p>
    <p>
    Contact Person Name :&nbsp; 
        <asp:TextBox ID="TextBox4" runat="server" Width="196px" ></asp:TextBox>
    </p>
     <p>
        <asp:TextBox ID="TextBox5" runat="server" ></asp:TextBox>
    </p>
     <p>
        <asp:TextBox ID="TextBox6" runat="server" ></asp:TextBox>
    </p>
     <p>
        <asp:TextBox ID="TextBox7" runat="server" ></asp:TextBox>
    </p>
</asp:Content>

在ASP.NET中选择两列DropDownList后,填充多个文本框

代码背后的代码。

 protected void LoadOptions()
    {
        DataTable CardCode = new DataTable();
        SqlConnection connection = new SqlConnection("my connection string");
        using (connection)
        {
            SqlDataAdapter adapter = new SqlDataAdapter("SELECT CardCode,CardName, Address, CntctPrsn FROM OCRD", connection);
            adapter.Fill(CardCode);
            if (CardCode.Rows.Count > 0)
            {
                for (int i = 0; i < CardCode.Rows.Count; i++)
                {
                     id = CardCode.Rows[i]["CardCode"].ToString();
                     name = CardCode.Rows[i]["CardName"].ToString();
                     newName = id + " ---- " + name;
                     DropDownList1.Items.Add(new ListItem(newName,id));
                }
            }
        }
    }

.Aspx代码

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="StackOver._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
    Welcome to ASP.NET!
    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
        onselectedindexchanged="DropDownList1_SelectedIndexChanged">
    </asp:DropDownList>
</h2>
       <br />
    <br />
<p>
    &nbsp;</p>
<p>
    &nbsp;</p>
<p>
Business Partner Code :&nbsp; 
    <asp:TextBox ID="TextBox1" runat="server" Width="192px" ></asp:TextBox>
</p>
<p>
Business Partner Name :
    <asp:TextBox ID="TextBox2" runat="server" Width="192px" ></asp:TextBox>
</p>
<p>
 Address :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
    <asp:TextBox ID="TextBox3" runat="server" Width="196px" ></asp:TextBox>
</p>
<p>
Contact Person Name :&nbsp; 
    <asp:TextBox ID="TextBox4" runat="server" Width="196px" ></asp:TextBox>
</p>
 <p>
    <asp:TextBox ID="TextBox5" runat="server" ></asp:TextBox>
</p>
 <p>
    <asp:TextBox ID="TextBox6" runat="server" ></asp:TextBox>
</p>
 <p>
    <asp:TextBox ID="TextBox7" runat="server" ></asp:TextBox>
</p>