参考链接:
http://www.cnblogs.com/zfanlong1314/p/3595298.html
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_131.WebForm1" %> <!DOCTYPE html> <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" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList> <br /> <asp:Label ID="Label1" runat="server" Text="Name"></asp:Label> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label2" runat="server" Text="Age"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <br /> <asp:Label ID="Label3" runat="server" Text="Remark"></asp:Label> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace _131 { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string ConStr = ConfigurationManager.ConnectionStrings["strCon"].ConnectionString; SqlConnection con = new SqlConnection(ConStr); SqlCommand cmd = new SqlCommand("select * from tb_sellInfo", con); if (con.State.Equals(ConnectionState.Closed)) { con.Open(); } SqlDataReader sqr = cmd.ExecuteReader(); if (sqr.HasRows) { while (sqr.Read()) { DropDownList1.Items.Add(sqr["Name"].ToString()); DropDownList1.DataTextField = "Name"; DropDownList1.DataBind(); } ListItem li = new ListItem("请选择", "0"); DropDownList1.Items.Insert(0, li); } sqr.Close(); con.Close(); } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { string sqlcmd = "select * from tb_sellInfo where Name='" + Convert.ToString(DropDownList1.SelectedValue)+"'"; string ConStr = ConfigurationManager.ConnectionStrings["strCon"].ConnectionString; SqlConnection con = new SqlConnection(ConStr); con.Open(); SqlCommand cmd = new SqlCommand(sqlcmd, con); SqlDataReader sqr = cmd.ExecuteReader(); sqr.Read(); TextBox1.Text = sqr["Name"].ToString(); TextBox2.Text = sqr["Age"].ToString(); TextBox3.Text = sqr["UpdateDate"].ToString(); sqr.Close(); con.Close(); } } }