Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)归海一刀published in(发表于) 2014/1/30 0:51:11 asp.net,2.0,客户端回调实现全国省市县3级联动下拉列表_[Asp.Net教程]
asp.net 2.0 客户端回调实现全国省市县3级联动下拉列表从网上看到一些相关的例子,发现代码较繁。最近自己修改了一下,贴出与君共勉。
客户端:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
无标题页 onclick='select("ddl1");'> onclick='select("ddl2");'>
服务器端:
using System;using System.Data;using System.Configuration;using System.Collections;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;
public partial class Default4 : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler{ private System.Data.SqlClient.SqlCommand sqlSelectCommand1; private System.Data.SqlClient.SqlConnection sqlConnection1; private System.Data.SqlClient.SqlDataAdapter da_pro; private System.Data.SqlClient.SqlCommand sqlSelectCommand2; private System.Data.SqlClient.SqlCommand sqlSelectCommand3; private System.Data.SqlClient.SqlDataAdapter da_dis; private System.Data.SqlClient.SqlDataAdapter da_city; private ds ds1; private string ret;
protected void Page_Load(object sender, EventArgs e) { InitializeComponent(); da_pro.Fill(ds1); itemadd(ds1.province.DefaultView, ddl1); String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); String callbackScript; callbackScript = "function CallTheServer(arg,context)" + "{ " + cbReference + "} ;"; Page.ClientScript.RegisterStartupScript(this.GetType(), "abcdefg", callbackScript, true); }
private void InitializeComponent() { this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand(); this.sqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.da_pro = new System.Data.SqlClient.SqlDataAdapter(); this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand(); this.da_city = new System.Data.SqlClient.SqlDataAdapter(); this.sqlSelectCommand3 = new System.Data.SqlClient.SqlCommand(); this.da_dis = new System.Data.SqlClient.SqlDataAdapter(); this.ds1 = new ds(); ((System.ComponentModel.ISupportInitialize)(this.ds1)).BeginInit(); // // sqlSelectCommand1 // this.sqlSelectCommand1.CommandText = "SELECT provinceID, province FROM province"; this.sqlSelectCommand1.Connection = this.sqlConnection1; // // sqlConnection1 // this.sqlConnection1.ConnectionString = "Data Source=localhost,2433;Initial Catalog=cargo;Persist Security Info=True;User " + "ID=sa;Password=alan"; this.sqlConnection1.FireInfoMessageEventOnUserErrors = false; // // da_pro // this.da_pro.SelectCommand = this.sqlSelectCommand1; this.da_pro.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "province", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("provinceID", "provinceID"), new System.Data.Common.DataColumnMapping("province", "province")})}); // // sqlSelectCommand2 // this.sqlSelectCommand2.CommandText = "SELECT cityID, city FROM city WHERE (father = @pro)"; this.sqlSelectCommand2.Connection = this.sqlConnection1; this.sqlSelectCommand2.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("@pro", System.Data.SqlDbType.NVarChar, 6, "father")}); // // da_city // this.da_city.SelectCommand = this.sqlSelectCommand2; this.da_city.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "city", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("cityID", "cityID"), new System.Data.Common.DataColumnMapping("city", "city")})}); // // sqlSelectCommand3 // this.sqlSelectCommand3.CommandText = "SELECT areaID, area FROM area WHERE (father = @city)"; this.sqlSelectCommand3.Connection = this.sqlConnection1; this.sqlSelectCommand3.Parameters.AddRange(new System.Data.SqlClient.SqlParameter[] { new System.Data.SqlClient.SqlParameter("@city", System.Data.SqlDbType.NVarChar, 6, "father")}); // // da_dis // this.da_dis.SelectCommand = this.sqlSelectCommand3; this.da_dis.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "area", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("areaID", "areaID"), new System.Data.Common.DataColumnMapping("area", "area")})}); // // ds1 // this.ds1.DataSetName = "ds"; this.ds1.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema; ((System.ComponentModel.ISupportInitialize)(this.ds1)).EndInit();
}
private void itemadd(DataView dv, DropDownList ddl) { while (ddl.Items.Count > 1) ddl.Items.RemoveAt(ddl.Items.Count - 1); foreach (DataRowView drv in dv) { ListItem li = new ListItem(); li.Text = drv[1].ToString(); li.Value = drv[0].ToString(); ddl.Items.Add(li); } } public void RaiseCallbackEvent(string eventarg) { string getparm = eventarg; string[] sinfo = getparm.Split(','); switch (sinfo[0]) { case "ddl1": { da_city.SelectCommand.Parameters[0].Value = sinfo[1]; da_city.Fill(ds1); ret="ddl2,"; foreach (DataRow row in ds1.city) { ret += row["cityID"] + "@" + row["city"] + ","; } break; } case "ddl2": { da_dis.SelectCommand.Parameters[0].Value = sinfo[1]; da_dis.Fill(ds1); ret = "ddl3,"; foreach (DataRow row in ds1.area) { ret += row["areaID"] + "@" + row["area"] + ","; } break; } case "ddl3": { break; } } ret.Remove(ret.Length - 1); }
public string GetCallbackResult() { return ret; }
实现效果:
数据库采用 csdn里可以得到的 area.mdb 转入sqlsrv
来源:csdn
赞