asp.net 2.0 客户端回调实现全国省市县3级联动下拉列表
从网上看到一些相关的例子,发现代码较繁。最近自己修改了一下,贴出与君共勉。
\n
客户端:
\n
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default4.aspx.cs” Inherits=”Default4″ %>
\n
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
\n
<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>无标题页</title>
</head>
<script language=javascript>
function select(select)
{
var lb = document.getElementById(select);
var con = lb.options[lb.selectedIndex].value;
//控件标示+,+值
con=select+”,”+con;
CallTheServer(con,”);
}
function ReceiveServerData(result)
{
var arrData = result.split(“,”);
var item=null
selectTag = document.getElementById(arrData[0]);
selectTag.length=0;
for (var i = 1; i < arrData.length; i++)
{
var data = arrData[i].split(“@”);
item=new Option(data[1],data[0]);
selectTag.options.add(item);
if(arrData[0] !=”ddl3″)
{
selectTag.selectedIndex=0;
select(arrData[0]);
}
}
}
</script>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:DropDownList ID=”ddl1″ runat=”server” Width=”94px” onclick=’select(“ddl1″);’>
</asp:DropDownList>
<asp:DropDownList ID=”ddl2″ runat=”server” Width=”94px” onclick=’select(“ddl2″);’>
</asp:DropDownList>
<asp:DropDownList ID=”ddl3″ runat=”server” Width=”94px”>
</asp:DropDownList></div>
</form>
</body>
</html>
\n
服务器端:
\n
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;
\n
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;
\n
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);
}
\n
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();
\n
}
\n
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);
}
\n
public string GetCallbackResult()
{
return ret;
}
\n
}
\n
实现效果:
\n\n
数据库采用 csdn里可以得到的 area.mdb 转入sqlsrv
\n
来源:csdn
\n