1、定义一个类 CityCounty.cs文件,如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Runtime.Serialization;
- /// <summary>
- ///CityCounty 的摘要说明
- /// </summary>
- [DataContract]
- publicclass CityCounty
- {
- [DataMember]
- privateint menu_ID;
- publicint Menu_ID
- {
- get { return menu_ID; }
- set { menu_ID = value; }
- }
- [DataMember]
- privatestring menu_Name;
- publicstring Menu_Name
- {
- get { return menu_Name; }
- set { menu_Name = value; }
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
/// <summary>
///CityCounty 的摘要说明
/// </summary>
[DataContract]
public class CityCounty
{
[DataMember]
private int menu_ID;
public int Menu_ID
{
get { return menu_ID; }
set { menu_ID = value; }
}
[DataMember]
private string menu_Name;
public string Menu_Name
{
get { return menu_Name; }
set { menu_Name = value; }
}
}
2、定义一个Json处理类,ToJson.cs文件,如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Runtime.Serialization.Json;
- using System.IO;
- /// <summary>
- ///JsonHelper 的摘要说明
- /// </summary>
- publicstaticclass JsonHelper
- {
- //转换为Json格式输出
- publicstaticstring ToJson(thisobject obj)
- {
- DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
- Stream stream = new MemoryStream();
- serializer.WriteObject(stream, obj);
- stream.Position = 0;
- StreamReader streamReader = new StreamReader(stream);
- return streamReader.ReadToEnd();
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization.Json;
using System.IO;
/// <summary>
///JsonHelper 的摘要说明
/// </summary>
public static class JsonHelper
{
//转换为Json格式输出
public static string ToJson(this object obj)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
Stream stream = new MemoryStream();
serializer.WriteObject(stream, obj);
stream.Position = 0;
StreamReader streamReader = new StreamReader(stream);
return streamReader.ReadToEnd();
}
}
3、定义Default4.aspx及Default4.aspx.cs文件,如下:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- <script src="jquery.js" type="text/javascript"></script>
- <script type="text/javascript" language="javascript">
- $(function () {
- $("#DropDownList1").change(function () {
- $.ajax({
- url: "Default5.aspx?id=" + $(this).val(),
- data: null,
- dataType: "json",
- success: function (data) {
- $("#DropDownList2").empty();
- //第一种方法
- //for (var i = 0; i < data.length; i++) {
- //$("<option value='" + data[i]["menu_ID"] + "'>" + data[i]["menu_Name"] + "</option>").appendTo("#DropDownList2");
- //}
- //第二种方法 用下面的方法也能够循环输出 .each方法
- $.each(data, function (i) {
- $("<option value='" + data[i]["menu_ID"] + "'>" + data[i]["menu_Name"] + "</option>").appendTo("#DropDownList2");
- })
- }
- });
- });
- });
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <div>
- <asp:Label ID="lblone" runat="server" Text="市" />
- <asp:DropDownList ID="DropDownList1" runat="server">
- </asp:DropDownList>
- <asp:Label ID="lbltwo" runat="server" Text="县" />
- <asp:DropDownList ID="DropDownList2" runat="server">
- <asp:ListItem Text="--请选择市--" Value="1"></asp:ListItem>
- </asp:DropDownList>
- </div>
- </div>
- </form>
- </body>
- </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(function () {
$("#DropDownList1").change(function () {
$.ajax({
url: "Default5.aspx?id=" + $(this).val(),
data: null,
dataType: "json",
success: function (data) {
$("#DropDownList2").empty();
//第一种方法
//for (var i = 0; i < data.length; i++) {
//$("<option value='" + data[i]["menu_ID"] + "'>" + data[i]["menu_Name"] + "</option>").appendTo("#DropDownList2");
//}
//第二种方法 用下面的方法也能够循环输出 .each方法
$.each(data, function (i) {
$("<option value='" + data[i]["menu_ID"] + "'>" + data[i]["menu_Name"] + "</option>").appendTo("#DropDownList2");
})
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<asp:Label ID="lblone" runat="server" Text="市" />
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:Label ID="lbltwo" runat="server" Text="县" />
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Text="--请选择市--" Value="1"></asp:ListItem>
</asp:DropDownList>
</div>
</div>
</form>
</body>
</html>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using USTC;
- using System.Data;
- public partial class Default4 : System.Web.UI.Page
- {
- DM dm = new DM();
- protectedvoid Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- string strSQL = "select * from UDS_Menu where Menu_ID like '%____00%'";
- DataSet ds = dm.getsql(strSQL);
- this.DropDownList1.DataSource = ds;
- this.DropDownList1.DataTextField = "Menu_Name";
- this.DropDownList1.DataValueField = "Menu_ID";
- this.DropDownList1.DataBind();
- this.DropDownList1.Items.Insert(0,"--请选择城市--");
- }
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using USTC;
using System.Data;
public partial class Default4 : System.Web.UI.Page
{
DM dm = new DM();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strSQL = "select * from UDS_Menu where Menu_ID like '%____00%'";
DataSet ds = dm.getsql(strSQL);
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataTextField = "Menu_Name";
this.DropDownList1.DataValueField = "Menu_ID";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0,"--请选择城市--");
}
}
}
4、定义Default5.aspx及Default5.aspx.cs文件,如下:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="TEST_Default5" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- </div>
- </form>
- </body>
- </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="TEST_Default5" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using USTC;
- using System.Data;
- public partial class TEST_Default5 : System.Web.UI.Page
- {
- DM dm = new DM();
- protectedvoid Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- IList<CityCounty> list = new List<CityCounty>();
- string id = Request.QueryString["id"].ToString();
- string strSQL = "select Menu_ID,Menu_Name from UDS_Menu where Super_Menu_ID=" + int.Parse(id);
- DataSet ds = dm.getsql(strSQL);
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- CityCounty c = new CityCounty();
- c.Menu_ID = Convert.ToInt32(ds.Tables[0].Rows[i]["Menu_ID"].ToString());
- c.Menu_Name = ds.Tables[0].Rows[i]["Menu_Name"].ToString();
- list.Add(c);
- }
- Response.Write(list.ToJson());
- Response.End();
- }
- }
- }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using USTC;
using System.Data;
public partial class TEST_Default5 : System.Web.UI.Page
{
DM dm = new DM();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IList<CityCounty> list = new List<CityCounty>();
string id = Request.QueryString["id"].ToString();
string strSQL = "select Menu_ID,Menu_Name from UDS_Menu where Super_Menu_ID=" + int.Parse(id);
DataSet ds = dm.getsql(strSQL);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
CityCounty c = new CityCounty();
c.Menu_ID = Convert.ToInt32(ds.Tables[0].Rows[i]["Menu_ID"].ToString());
c.Menu_Name = ds.Tables[0].Rows[i]["Menu_Name"].ToString();
list.Add(c);
}
Response.Write(list.ToJson());
Response.End();
}
}
}