主要学习代码:

List.html

    <script type="text/javascript">
function GetXhr() {
return new XMLHttpRequest();
} var getCities = function () {
//alert(this.options[this.selectedIndex].value);
//清空列表
var cities = document.getElementById("sltCity");
cities.options.length = 0; var id = this.options[this.selectedIndex].value;
var xhr = GetXhr();
xhr.open("post", "List.ashx", true);
//post方式添加
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//post方式永远不会使用浏览器的缓存
//设置不使用浏览器缓存
//xhr.setRequestHeader("If-Modified-Since", "0"); xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
var json = eval(xhr.response);
for (var i = 0; i < json.length; i++) {
var node=document.createElement("option");
node.innerHTML=json[i].Name;
cities.appendChild(node);
}
}
};
xhr.send("pId=" + id);
};
function getProvinces() {
//创建异步请求对象
var xhr = GetXhr();
// alert(xhr.readyState);//0
//设置参数
xhr.open("get", "List.ashx", true); // alert(xhr.readyState);//1
xhr.onreadystatechange = function () {
// alert(xhr.readyState);//4
if (xhr.readyState == 4) {
if (xhr.status == 200) {
// var s = [{ id: 1, name: 'value' }];
var cont = eval(xhr.responseText);
// alert(cont[0]);
//alert(cont.length);
var slt = document.getElementById("sltProvince");
for (var i = 0; i < cont.length; i++) {
slt.options[i] = new Option(cont[i].Name, cont[i].ID);
}
} else {
alert("服务器繁忙,请稍后在试。");
}
}
};
//alert(xhr.readyState);//1
//发送请求
xhr.send();//post方式填写参数
//alert(xhr.readyState);//1
};
window.onload = function () {
getProvinces();
document.getElementById("sltProvince").onchange = getCities;
};
</script>
</head>
<body>
省:
<select id="sltProvince">
</select>
&nbsp;市:<select id="sltCity">
</select>
</body>

List.ashx:

<%@ WebHandler Language="C#" Class="List" %>

using System;
using System.Web;
using System.Collections.Generic;//List
//序列化命名空间
using System.Web.Script.Serialization; public class List : IHttpHandler
{ private Web.BLL.Transfer transfer = null;
private List<Web.Model.PC> lpc = null;
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
string id = context.Request.Form["pId"];
if (!string.IsNullOrEmpty(id))
{
//第二种方式
//市
transfer = new Web.BLL.Transfer();
JavaScriptSerializer serializer = new JavaScriptSerializer();
lpc = transfer.GetProvincesOrCities(int.Parse(id));
//这样返回的json自动以属性为键。
//[{"Name":"白城市","Id":94},
string json = serializer.Serialize(lpc);
context.Response.Write(json);
}
else
{
//第一种方式
//省
transfer = new Web.BLL.Transfer();
lpc = transfer.GetProvincesOrCities(0);
System.Text.StringBuilder sb = new System.Text.StringBuilder(500);
//拼接json字符串
sb.Append("[");
foreach (Web.Model.PC item in lpc)
{
sb.Append("{ID:" + item.Id + ",Name:'" + item.Name + "'},");
}
sb.Remove(sb.Length - 1, 1).Append("]");
context.Response.Write(sb.ToString());
}
}

Web.BAL

    public class DataAction
{
private List<Web.Model.PC> lpc = null;
private Web.Model.PC pc = null;
public DataAction()
{
lpc = new List<Web.Model.PC>();
} public List<Web.Model.PC> GetProvincesOrCities(int pId)
{
string sql = "select * from TblArea where AreaPId={0}";
sql = string.Format(sql, pId);
System.Data.DataTable dt = SqlHelper.ExecuteDataTable(sql);
if (dt.Rows.Count > )
{
foreach (System.Data.DataRow item in dt.Rows)
{
pc = new Web.Model.PC(int.Parse(item[].ToString()), item[].ToString());
lpc.Add(pc);
}
}
return lpc;
}
}

项目文件:http://pan.baidu.com/s/1hq9Ro7E

最新文章

  1. django博客功能实现——标签功能
  2. Vue.js线程机制问题还是数据双向绑定有延迟的问题
  3. 关于Vector中的元素中含有指针成员的情况
  4. 批处理命令——for
  5. UITextView
  6. 1121 if条件语句练习--输入年月日判断执行
  7. 关于完整解答Leo C.W博客中名为“我们公司的ASP.NET 笔试题,你觉得难度如何”的所有题目
  8. iOS设置某个界面强制横屏,进入就横屏
  9. 面试之BI-SQL--table转换
  10. Android学习笔记(SQLite的简单使用)
  11. 素数判定 AC 杭电
  12. jquery中ajax的用法
  13. 【Howie玩docker】-Docker常用命令操作
  14. iOS8 UITableView 分割条设置separator intent = 0 不起作用
  15. 【翻译自mos文章】v$undostat视图没有依照每10分钟进行更新,v$undostat仅仅有1行(one rows)
  16. Android手机外置SD卡(TF卡)的获取方法
  17. Simditor使用方法
  18. Spring Data JPA: 实现自定义Repository
  19. php注册登录源代码
  20. 小妖精的完美游戏教室——东方PROJECT,同人,符卡系统

热门文章

  1. 批处理快速创建wifi
  2. pdo封装类
  3. 【转】C# HttpWebRequest\HttpWebResponse\WebClient发送请求解析json数据
  4. The Producer-Consumer Relationship Version 2
  5. nrf51822-提高nordic ble数据发送速率
  6. mssql 常用SQL语句或函数
  7. UML时序图总结
  8. Redis Sentinel高可用配置及C#访问
  9. Bone Collector
  10. opencv hog+svm行人检测