我有一个链接到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>
答案 0 :(得分:1)
将代码中的代码替换为代码:
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>
答案 1 :(得分:0)
首先,您还需要在下拉列表中存储卡片代码,而不仅仅是卡片名称
DropDownList1.DataValueField = "CardCode";
然后,在填充文本框时,您没有指定WHERE过滤器。试试这个:
SqlCommand theCommand = new SqlCommand("SELECT CardCode, CardName, Address, CntctPrsn FROM OCRD WHERE CardCode = @CardCode", connection);