在ASP.NET中从DropDownList中选择数据后,填充多个文本框
本文关键字:填充 文本 数据 NET ASP 中从 DropDownList 选择 | 更新日期: 2023-09-27 18:24:06
我有一个链接到SQL数据库的DropDownList。它当前显示了一个CardCode列表。我正在努力使其在选择CardCode后,会自动填充多个文本框(如CardNum、CntctPerson、ListNum等)。我可以自动填充"CardCode"和CardNum文本框,因为该值是所选的值,但它显示数据库中的第一行,我想显示与CardCode相关的行,我不知道如何填充与所选CardCode有关的其他行。我该怎么做?提前感谢
这是下面的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 here);
using (connection)
{
SqlDataAdapter adapter = new SqlDataAdapter("SELECT CardCode, CardName, Address, CntctPrsn FROM OCRD", connection);
adapter.Fill(CardCode);
// DropDownList1.DataSource = customers;
DropDownList1.DataTextField = "CardName";
DropDownList1.DataValueField = "CardName";
DropDownList1.DataBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string selected = DropDownList1.SelectedItem.Value;
SqlConnection connection = new SqlConnection(my conection here);
using (connection)
{
SqlCommand theCommand = new SqlCommand("SELECT CardCode, CardName, Address, CntctPrsn FROM OCRD", 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.TextBox4.Text = theReader.GetString(3);
// TextBox5.Text = theReader.GetString(4);
// TextBox6.Text = theReader.GetString(5);
// TextBox7.Text = theReader.GetString(6);
}
connection.Close();
}
}
}
}
这也是我的.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="CardCode"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:myconnection string here %>"
SelectCommand="SELECT [CardCode], [CardName], [Address], [CntctPrsn] FROM [OCRD]">
</asp:SqlDataSource>
</h2>
<p>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
</p>
<p>
<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
</p>
<p>
<asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
</p>
<p>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</p>
</asp:Content>
在代码隐藏代码中替换以下代码:
DropDownList1.DataValueField = "CardCode";
SqlCommand theCommand = new SqlCommand("SELECT CardCode, CardName, Address, CntctPrsn FROM OCRD where CardCode=@CardCode ", connection);
您使用代码隐藏和数据源在两侧绑定下拉菜单,因此删除Sql数据源,如下所示:
<%@ 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>
<p>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
</p>
<p>
<asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
</p>
<p>
<asp:TextBox ID="TextBox3" runat="server" ></asp:TextBox>
</p>
<p>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</p>
首先,您还需要在下拉列表中存储卡代码,而不仅仅是卡名
DropDownList1.DataValueField = "CardCode";
然后,在填充文本框时没有指定WHERE筛选器。试试这个:
SqlCommand theCommand = new SqlCommand("SELECT CardCode, CardName, Address, CntctPrsn FROM OCRD WHERE CardCode = @CardCode", connection);