asp.net下使用AjaxPro實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)代碼
更新時(shí)間:2010年10月14日 00:16:25 作者:
本文展示了如何利用AjaxPro與服務(wù)器交互,并且還展示了在Js中可以直接調(diào)用服務(wù)器返回的集合和直接調(diào)用服務(wù)器上class的屬性
復(fù)制代碼 代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!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>AjaxPro實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="200" border="0" align="center" cellpadding="3" cellspacing="1" bordercolor="#FFFFFF" style="border-collapse: collapse">
<tr align="center">
<td height="20" colspan="2">
<strong>AjaxPro實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)</strong> </td>
</tr>
<tr class="tdbg" >
<td width="30%">
省份</td>
<td width="70%" align="left">
<asp:DropDownList ID="ddlStateList" runat="server" DataTextField="StateName" DataValueField="StateId">
</asp:DropDownList></td>
</tr>
<tr class="tdbg" >
<td><strong>城市</strong></td>
<td align="left">
<asp:DropDownList ID="ddlCityList" runat="server">
</asp:DropDownList></td>
</tr>
</table>
</div>
<script language="javascript" type="text/javascript" defer="defer">
function ShowCity(id)
{
var res=Test.GetCityList(parseInt(id)).value;
var ddl=document.getElementById("<%=ddlCityList.UniqueID %>");
ddl.length=0;
if(res)
{
//res是服務(wù)器返回的一個(gè)List<City>集合
for(var i=0;i<res.length;i++)
{
ddl.options.add(new Option(res[i].CityName,res[i].CityId));
//從上面可以看出可以直接調(diào)用List<City>集合中的元素和它們的屬性
}
}
}
</script>
</form>
</body>
</html>
<DIV class=cnblogs_Highlighter><PRE class=brush:csharp>using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
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;
/**
* 寫作說明:本文展示了如何利用AjaxPro與服務(wù)器交互,并且還展示了在Js中可以直接調(diào)用服務(wù)器返回的集合和直接調(diào)用服務(wù)器上class的屬性
* 作者:周公
* 日期:2008-1-1
* 首發(fā)地址:http://blog.csdn.net/zhoufoxcn/
**/
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
List<State> stateList = new List<State>(10);
stateList.Add(new State(0, "選擇城市"));//默認(rèn)選項(xiàng)
stateList.Add(new State(1,"北京"));
stateList.Add(new State(2, "天津"));
stateList.Add(new State(3, "上海"));
stateList.Add(new State(4, "湖北"));
stateList.Add(new State(5, "湖南"));
stateList.Add(new State(6, "山西"));
ddlStateList.DataSource = stateList;
ddlStateList.DataBind();
ddlStateList.Attributes["onchange"] = "ShowCity(this.options[selectedIndex].value)";
}
AjaxPro.Utility.RegisterTypeForAjax(typeof(Test));//注冊(cè)
}
[AjaxPro.AjaxMethod]
public List<City> GetCityList(int stateId)
{
//呵呵,都是我熟悉的城市或者區(qū)
List<City> cityList = new List<City>(12);
cityList.Add(new City(11, "海淀區(qū)", 1));
cityList.Add(new City(12, "朝陽區(qū)", 1));
cityList.Add(new City(13, "大港區(qū)", 2));
cityList.Add(new City(14, "南開區(qū)", 2));
cityList.Add(new City(15, "普陀區(qū)", 3));
cityList.Add(new City(16, "黃浦區(qū)", 3));
cityList.Add(new City(17, "黃岡市", 4));
cityList.Add(new City(18, "荊州市", 4));
cityList.Add(new City(19, "長(zhǎng)沙市", 5));
cityList.Add(new City(20, "岳陽市", 5));
cityList.Add(new City(21, "太原市", 6));
cityList.Add(new City(22, "大同市", 6));
List<City> tempList = new List<City>();
for (int i = 0; i < cityList.Count; i++)
{
if (cityList[i].StateId == stateId)
{
tempList.Add(cityList[i]);
}
}
return tempList;
}
}
/// <summary>
/// 省份信息
/// </summary>
public class State
{
private int stateId;
private string stateName;
/// <summary>
/// 省份名
/// </summary>
public string StateName
{
get { return stateName; }
set { stateName = value; }
}
/// <summary>
/// 省份編號(hào)
/// </summary>
public int StateId
{
get { return stateId; }
set { stateId = value; }
}
public State(int stateId, string stateName)
{
this.stateId = stateId;
this.stateName = stateName;
}
}
/// <summary>
/// 城市信息
/// </summary>
public class City
{
private int cityId;
private int stateId;
private string cityName;
/// <summary>
/// 城市名稱
/// </summary>
public string CityName
{
get { return cityName; }
set { cityName = value; }
}
/// <summary>
/// 城市所在省份編號(hào)
/// </summary>
public int StateId
{
get { return stateId; }
set { stateId = value; }
}
/// <summary>
/// 城市編號(hào)
/// </summary>
public int CityId
{
get { return cityId; }
set { cityId = value; }
}
public City(int cityId, string cityName, int stateId)
{
this.cityId = cityId;
this.cityName = cityName;
this.stateId = stateId;
}
}
</PRE>
</DIV>
相關(guān)文章
ASP.NET數(shù)組刪除重復(fù)值實(shí)現(xiàn)代碼
在ASP.NET編程中,要想刪除數(shù)組的重復(fù)值可以使用多種方法代碼實(shí)現(xiàn)相同的效果。今天,在某個(gè)博客中看到某功能代碼中的一小段代碼很不錯(cuò),它就是用來移動(dòng)數(shù)組中相同值的方法,分享給大家2015-10-10asp.net實(shí)現(xiàn)的DES加密解密操作示例
這篇文章主要介紹了asp.net實(shí)現(xiàn)的DES加密解密操作,結(jié)合具體實(shí)例形式分析了asp.net實(shí)現(xiàn)DES加密與解密算法的實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-07-07ASP.NET保存PDF、Word和Excel文件到數(shù)據(jù)庫
這篇文章主要為大家詳細(xì)介紹了ASP.NET保存PDF、Word和Excel文件到數(shù)據(jù)庫的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Asp.net生成Excel文件并下載(更新:解決使用迅雷下載頁面而不是文件的問題)
Asp.net生成Excel文件并下載(更新:解決使用迅雷下載頁面而不是文件的問題)2012-01-01.NET?Core企業(yè)微信開發(fā)接口回調(diào)配置
這篇文章介紹了.NET?Core企業(yè)微信回調(diào)配置的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06ASP.NET Core 3.0輕量級(jí)角色API控制授權(quán)庫
這篇文章介紹了ASP.NET Core 3.0輕量級(jí)角色API控制授權(quán)庫,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01