AJAX三级联动

WebService后台:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace ProLianDong
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{


[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string bindpr()
{
string str = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
SqlConnection cnn = new SqlConnection(str);
SqlCommand cmm = cnn.CreateCommand();
cmm.CommandText = "select * from province";
SqlDataAdapter sda = new SqlDataAdapter(cmm);
DataSet dt = new DataSet();
sda.Fill(dt);
string opstr=null;
for (int i = 0; i <dt.Tables[0].Rows.Count; i++)
{

opstr += "<option value=" + dt.Tables[0].Rows[i][1].ToString() + ">" + dt.Tables[0].Rows[i][2].ToString() + "</option>";

}
return opstr;
}
[WebMethod]
public string bindct(int prid)
{
string str = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
SqlConnection cnn = new SqlConnection(str);
SqlCommand cmm = cnn.CreateCommand();
cmm.CommandText = "select * from city where father="+prid;
SqlDataAdapter sda = new SqlDataAdapter(cmm);
DataSet dt = new DataSet();
sda.Fill(dt);
string ctstr = null;
for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
{

ctstr += "<option value=" + dt.Tables[0].Rows[i][1].ToString() + ">" + dt.Tables[0].Rows[i][2].ToString() + "</option>";

}
return ctstr;
}
[WebMethod]
public string bindar(int ctid)
{
string str = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
SqlConnection cnn = new SqlConnection(str);
SqlCommand cmm = cnn.CreateCommand();
cmm.CommandText = "select * from area where father=" + ctid;
SqlDataAdapter sda = new SqlDataAdapter(cmm);
DataSet dt = new DataSet();
sda.Fill(dt);
string arstr = null;
for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
{

arstr += "<option value=" + dt.Tables[0].Rows[i][1].ToString() + ">" + dt.Tables[0].Rows[i][2].ToString() + "</option>";

}
return arstr;
}
}
}

..................................................................................
HTML:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script src="Jquery1.7.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
bp();
$('#Select1').change(function () {
bc();
}),
$('#Select2').change(function () {
ba();
});
function bp() {
ProLianDong.WebService1.bindpr(onSuccess, onFailed);
function onSuccess(p) {

$('#Select1').html(p);

}
function onFailed() {

alert('失败!');
}
}
function bc() {
ProLianDong.WebService1.bindct($('#Select1').val(), onSuccess, onFailed);
function onSuccess(c) {
$('#Select2').html(c);
}
function onFailed() {

alert('失败!');
}
}
function ba() {
ProLianDong.WebService1.bindar($('#Select2').val(), onSuccess, onFailed);
function onSuccess(a) {

$('#Select3').html(a);

}
function onFailed() {

alert('失败!');
}
}
})
</script>
<style type="text/css">
#Select1
{
width: 89px;
}
#Select2
{
width: 101px;
}
#Select3
{
width: 177px;
}
#Text1
{
width: 142px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/WebService1.asmx" />
</Services>
</asp:ScriptManager>
<div>

姓名:<input id="Text1" type="text" /><br />
住址:<select id="Select1" name="D1">
<option></option>
</select><select id="Select2" name="D2">
<option></option>
</select><select id="Select3" name="D3">
<option></option>
</select></div>
</form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值