Ajax的DropDrowList应用——改变一个drp会引起另一个drp的值改变
发布时间:2020-12-16 02:00:56 所属栏目:百科 来源:网络整理
导读:通过Ajax技术对DropDrowList控件联动改变应用 改变一个DropDrowList会引起另一个DropDrowList的值改变: Page Code: table tr th省份/th td asp:DropDownList ID="drpProvice" runat="server" onselectedindexchanged="drpProvice_SelectedIndexChanged" Au
通过Ajax技术对DropDrowList控件联动改变应用 改变一个DropDrowList会引起另一个DropDrowList的值改变: Page Code: <table> <tr> <th>省份</th> <td> <asp:DropDownList ID="drpProvice" runat="server" onselectedindexchanged="drpProvice_SelectedIndexChanged" AutoPostBack="True"> </asp:DropDownList> </td> <th>城市</th> <td><asp:DropDownList ID="drpCity" runat="server"> </asp:DropDownList></td> </tr> </table> Class Code: protected void Page_Load(object sender,EventArgs e) { if (!IsPostBack) { InitData(); } } protected virtual Int32 StringToInt32(string val,Int32 defaultVal) { int tmp = 0; if (Int32.TryParse(val,out tmp)) { return tmp; } return defaultVal; } public SqlConnection Open() { string strConn = ConfigurationManager.ConnectionStrings["MySqlDataBase"].ConnectionString; SqlConnection conn = new SqlConnection(strConn); conn.Open(); return conn; } public void Close() { SqlConnection conn = Open(); conn.Close(); } private void InitData() { ProviceBind(); Citybind(); } private void ProviceBind() { string strSql = "select * from Ajax_Provice"; SqlConnection conn = Open(); SqlDataAdapter da = new SqlDataAdapter(strSql,conn); DataSet ds = new DataSet(); da.Fill(ds); drpProvice.DataSource = ds.Tables[0]; drpProvice.DataTextField = "c_provName"; drpProvice.DataValueField = "c_provId"; drpProvice.DataBind(); //drpTypeId.Items.Insert(0,new ListItem("全部","")); } private void Citybind() { int temp = StringToInt32(drpProvice.SelectedValue,0); string strSql = "select * from Ajax_City Where c_provId = {0}"; strSql = string.Format(strSql,temp); SqlConnection conn = Open(); SqlDataAdapter da = new SqlDataAdapter(strSql,conn); DataSet ds = new DataSet(); da.Fill(ds); drpCity.DataSource = ds.Tables[0]; drpCity.DataTextField = "c_city"; drpCity.DataValueField = "c_provId"; drpCity.DataBind(); Close(); } protected void drpProvice_SelectedIndexChanged(object sender,EventArgs e) { Citybind(); }当然只有这些代码,是不行的,要记住一点要把DropDrowList的属性 AutoPostBack设置为"True",不然不会有联动的效果哦。 数据库图: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |