日期:2014-05-18  浏览次数:20437 次

新人求教ASP.NET里的一个数据显示问题
我现在有个问题是这样的

我做了2个dropdownlist和2个textbox,数据库里是A,B,C,D四个列名

现在我想这样做

在第一个DropDownList里显示A列的内容
然后在A列的内容显示出来的同时,第2个DropDownList和2个TextBox显示出相应的数据来(也就是B,C,D列的内容来)

请假该怎么做呢...............(俺是用C#的)



------解决方案--------------------
第一步:创建数据库
CREATE TABLE [ABCD] (
[A] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[B] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[C] [char] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[D] [char] (10) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
第二步:代码
<%@ Page Language= "C# " Debug= "true "%>
<%@ Import Namespace= "System.Data " %>
<%@ Import Namespace= "System.Data.SqlClient " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">
string strCon = "Data Source=127.0.0.1;Database=test;Uid=sa;Pwd= ";
//string strCon = "Data Source=(local);Database=数据库名;Uid=帐号;Pwd=密码 ";

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
bind();
bind1();
}
}
public void bind()
{
SqlConnection con = new SqlConnection(strCon);
con.Open();
SqlCommand com = new SqlCommand( "select * from ABCD ",con);
SqlDataReader str = com.ExecuteReader();
this.DropDownList1.DataSource = str;
this.DropDownList1.DataTextField = "A ";
this.DropDownList1.DataValueField = "A ";
this.DropDownList1.DataBind();
con.Close();
}
public void bind1()
{
string strddl = this.DropDownList1.Text;
SqlConnection con = new SqlConnection(strCon);
con.Open();
string slt = "select B,C,D from ABCD where A= ' " + strddl + " ' ";
SqlDataAdapter da1 = new SqlDataAdapter(slt, con);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "ABCD ");
this.DropDownList2.Items[0].Text = ds1.Tables[ "ABCD "].Rows[0].ItemArray[0] + " ";
string str1 = ds1.Tables[ "ABCD "].Rows[0].ItemArray[0] + " ";
this.TextBox1.Text = ds1.Tables[ "ABCD "].Rows[0].ItemArray[1] + " ";
this.TextBox2.Text = ds1.Tables[ "ABCD "].Rows[0].ItemArray[2] + " ";
}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
bind1();
}
</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:DropDownList ID= "DropDownList1 " runat= "server " AutoPostBack= "True " OnSelectedIndexChanged= "DropDownList1_SelectedIndexChanged ">
</asp:DropDownList>
<asp:DropDownList ID= "DropDownList2 " runat= "server ">
<asp:ListItem> </asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID= "TextBox1 &quo