首页 > 编程 > .NET > 正文

asp.net下使用AjaxPro实现二级联动代码

2024-07-10 12:42:00
字体:
来源:转载
供稿:网友
代码如下:
<%@ 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实现二级联动</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实现二级联动</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是服务器返回的一个List<City>集合
for(var i=0;i<res.length;i++)
{
ddl.options.add(new Option(res[i].CityName,res[i].CityId));
//从上面可以看出可以直接调用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与服务器交互,并且还展示了在Js中可以直接调用服务器返回的集合和直接调用服务器上class的属性
* 作者:周公
* 日期:2008-1-1
* 首发地址: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, "选择城市"));//默认选项
stateList.Add(new State(1,"北京"));
stateList.Add(new State(2, "天津"));
stateList.Add(new State(3, "上海"));
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表