<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></div>
</form>
</body>
===============
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.DropDownList1.DataSource = //从数据库区的dataSet
this.DropDownList1.DataTextField = "姓名";
this.DropDownList1.DataValueField = "籍贯";
this.DropDownList1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string str = this.DropDownList1.SelectedValue; //这行重要..... 取选的项的值.
if(str == null || str.Length==0)
{
str = this.DropDownList1.Items[0].Value;
}
this.TextBox1.Text = str;
}
}
****************************
----- 下面是另一种方法
动态绑定数据库中的字段。
C#代码
1. SqlConnection conn = UtilitySqlClass.OperateDataBase.ReturnConn();
2. string strSQL = "select * from CompanyType";
3. SqlDataAdapter ada = new SqlDataAdapter(strSQL, conn);
4. DataSet ds = new DataSet();
5. ada.Fill(ds, "CompanyType");
6. DropDownList1.DataSource = ds.Tables["CompanyType"].DefaultView;
7. DropDownList1.DataValueField = ds.Tables["CompanyType"].Columns[1].ColumnName; //就是为了看这行......
8. DropDownList1.DataTextField = ds.Tables["CompanyType"].Columns[1].ColumnName;
9. DropDownList1.DataBind();
10. ds.Dispose();
************