c#里dropdownlist和Textbox关联的问题!
usingSystem;usingSystem。Data;usingSystem。Configuration;usingSystem。Collections;usingSystem。Web;usingSystem。Web。Security;usingSystem。
Web。UI;usingSystem。Web。UI。WebControls;usingSystem。Web。UI。WebControls。WebParts;usingSystem。Web。UI。HtmlControls;usingSystem。
Data。SqlClient;publicpartialcla {protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidButton1_Click(objectsender,EventArgse){stringConnString="server=。
;database=service;userid=sa;pwd=";SqlConnectionconn=newSqlConnection(ConnString);SqlCommandcomm=newSqlCommand();comm。Connection=conn;conn。
Open();if(TextBox8。Text==""){Response。Write("<script>alert('客户姓名不能为空!')</script>");}else{comm。CommandText="select*fromsuopeiwherespr='"+TextBox8。
Text+"'";SqlDataAdaptersda=newSqlDataAdapter(comm);DataSetds=newDataSet();sda。Fill(ds);if(ds。Tables[0]。Rows。Count>0){this。
DropDownList1。DataSource=ds;this。DropDownList1。DataValueField="spid";//selectvaluethis。DropDownList1。DataTextField="";//selecttext对应的数据库字段,下拉框显示的值,自己确定this。
DropDownList1。DataBind();}else{Response。Write("<script>alert('不存在该客户的信息,请核对后重新输入!')</script>");}conn。Close();}}protectedvoidDropDownList1_SelectedIndexChanged(objectsender,EventArgse){SqlConnectionconn=newSqlConnection(ConnString);stringpid=DropDownList1。
SelectedValue;SqlCommandcomm=newSqlCommand();comm。Connection=conn;conn。Open();comm。CommandText="select*fromsuopeiwherespid="+pid;SqlDataReadersdr=comm。
ExecuteReader();if(sdr。Read()){Label2。Text=sdr["djrq"]。ToString();Label9。Text=sdr["spid"]。ToString();Label11。Text=sdr["clxh"]。
ToString();TextBox5。Text=sdr["spyy"]。ToString();TextBox6。Text=sdr["cljg"]。ToString();TextBox7。Text=sdr["khyj"]。ToString();Label12。
Text=sdr["slr"]。ToString();}sdr。Close();conn。Close();}}。
DropDownList1.SelectedValue = sdr["spid"].ToString(); 这里你写错了,你是要将查询出来的ID号绑定到DropDownList中去,DropDownList1.SelectedValue这个方法肯定不行,你可以这样:DropDownList1.Items.Add(sdr["spid"].ToString());后面的就没有错了~~
用户名 和 保单 是 1对多所以 select * from tablename where 用户名 = 'x' 这个弄出来的数据读取器应该是多个数据所以应该用while(sdr.Read()){ DropDownList1.Items.Add(sde["字段"].ToString);}这个是绑定,然后当选择项改变的时候重新根据这个值搜索。因为不了解你的数据库所以不好具体说大概就是 select * from tablename where id ='xxx'id代表的表单应该是唯一的 读出来就可以了。
在TextChanged函数中执行就行啦,我做了一个textbox输入数据回车后就可以更新DropDownlist里的数据。如果要及时显示,不回车,就用ajax就行啦,加个ScriptManager和UpdatePanel就可以啦。using System;using System。
Data;using System。Configuration;using System。Web;using System。Web。Security;using System。Web。UI;using System。Web。UI。WebControls;using System。
Web。UI。WebControls。WebParts;using System。Web。UI。HtmlControls;using System。Data。OracleClient;public partial class _Default : System。
Web。UI。Page { protected void Page_Load(object sender, EventArgs e) { } protected void TextBox1_TextChanged(object sender, EventArgs e) { if (TextBox1。
Text != "") { string conn_str = "Data Source=****;user=**;password=**"; using (OracleDataAdapter oda = new OracleDataAdapter("select a。
times from tb_test a where tel='123'", conn_str)) { DataTable dt = new DataTable(); oda。
Fill(dt); for (int i = 0; i < dt。Rows。Count; i++) { DropDownList1。Items。
Add(dt。Rows[i][0]。ToString()); } } } }}不知道是否对你有用~~~~。
答:用DropDownList控件绑定XML数据实现省市区三级联动(需要用到VS2005的Datasource和AJAX1.0),此方法仅用于学习讨论.. City...详情>>