<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="AjaxTest.WebForm1" %>
<HTML>
<HEAD>
<title>Ajax</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
<SCRIPT language="javascript">
function cityResult() 

var city=document.getElementById("DropDownList1");
AjaxMethod.GetCityList(city.value,get_city_Result_CallBack);
}
function get_city_Result_CallBack(response)
{
if (response.value != null)
{
document.alinkColor("DropDownList2").length=0;
         var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
{
for(var i=0;i<ds.Tables[0].Rows.Count;i++)
         {
         var name=ds.Tables[0].Rows[i].city;
                            var id=ds.Tables[0].Rows[i].cityID;
                            document.all("DropDownList2").options.add(new Option(name,id));
         }
}
}
return
}
function areaResult() 

var area=document.getElementById("DropDownList2");
AjaxMethod.GetAreaList(area.value,get_area_Result_CallBack);
}
function get_area_Result_CallBack(response)
{
if (response.value != null)
{
document.all("DropDownList3").length=0;
var ds = response.value;
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
{
for(var i=0;i<ds.Tables[0].Rows.Count;i++)
          {
                            var name=ds.Tables[0].Rows[i].area;
                            var id=ds.Tables[0].Rows[i].areaID;
                            document.all("DropDownList3").options.add(new Option(name,id));
         }
}
}
return
}
function getData()
{
var province=document.getElementById("DropDownList1");
var pindex = province.selectedIndex;
var pValue = province.options[pindex].value;
var pText  = province.options[pindex].text;

var city=document.getElementById("DropDownList2");
var cindex = city.selectedIndex;
var cValue = city.options[cindex].value;
var cText  = city.options[cindex].text;

var area=document.getElementById("DropDownList3");
var aindex = area.selectedIndex;
var aValue = area.options[aindex].value;
var aText  = area.options[aindex].text;

var txt=document.getElementById("TextBox1"); document.getElementById("<%=TextBox1.ClientID%>").innerText="省: ("+pValue+")"+pText+"----市: ("+cValue+")"+cText+"----區(縣): ("+aValue+")"+aText;
}
</SCRIPT>
</HEAD>
<body ms_positioning="GridLayout">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 32px" cellSpacing="1"
cellPadding="1" width="300" border="1">
<TR>
<TD>省</TD>
<TD><asp:dropdownlist id="DropDownList1" runat="server"></asp:dropdownlist></TD>
</TR>
<TR>
<TD style="HEIGHT: 7px">市</TD>
<TD style="HEIGHT: 7px"><asp:dropdownlist id="DropDownList2" runat="server"></asp:dropdownlist></TD>
</TR>
<TR>
<TD>區(縣)</TD>
<TD><asp:dropdownlist id="DropDownList3" runat="server"></asp:dropdownlist></TD>
</TR>
</TABLE>
<asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 96px; POSITION: absolute; TOP: 144px" runat="server"
Width="520px"></asp:TextBox><INPUT style="Z-INDEX: 103; LEFT: 96px; WIDTH: 56px; POSITION: absolute; TOP: 176px; HEIGHT: 24px"
type="button" value="輸出" onclick="getData();">
</form>
</body>
</HTML>cs代碼如下:using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace AjaxTest
{
/// <summary>
/// Summary description for AjaxMethod.
/// </summary>
public class AjaxMethod
{ #region GetPovinceList
public static DataSet GetPovinceList()
{
string sql="select * from province";
return GetDataSet(sql);
}
#endregion #region GetCityList
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public  DataSet GetCityList(int povinceid)
{
string sql="select * from city where father="+povinceid;
return GetDataSet(sql);
}
#endregion #region GetAreaList
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
public  DataSet GetAreaList(int cityid)
{
string sql="select * from area where father="+cityid;
return GetDataSet(sql);
}
#endregion

#region GetDataSet
public static DataSet GetDataSet(string sql)
{
OleDbConnection conn=new OleDbConnection(@"Provider=Microsoft.Jet.OleDb.4.0;Data Source=F:\asp.net\AjaxTest\area.mdb;");
OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);
DataSet ds=new DataSet();
da.Fill(ds);
return ds;
}
#endregion
}
}
private void Page_Load(object sender, System.EventArgs e)
{
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));
if(!Page.IsPostBack)
{
this.DropDownList1.DataSource=AjaxMethod.GetPovinceList();
this.DropDownList1.DataTextField="province";
this.DropDownList1.DataValueField="provinceID";
this.DropDownList1.DataBind();
this.DropDownList1.Attributes.Add("OnSelectedIndexChanged","cityResult();");
this.DropDownList2.Attributes.Add("OnSelectedIndexChanged","areaResult();");
}
}

解决方案 »

  1.   

    说出你的错误是什么情况阿,大家都很忙的看代码太累,如果是因为Session或Cookie的传值问题的话,那你用表单验证可以解决
      

  2.   

    我是想要點擊DropDownList1(省的名稱)時DropDownList2就顯示(相應的市名)
    我是想要點擊DropDownList2(市的名稱)時DropDownList3就顯示(相應的區(縣)名)
    現在就是點擊了沒反應
      

  3.   

    把Ajax.Utility.RegisterTypeForAjax(typeof(AjaxMethod));改成AjaxPro.Utility.RegisterTypeForAjax(typeof(WebForm1));试一下
      

  4.   

    里面的参数是Page,不是AjaxMethod,其它错误还没找到
      

  5.   

    不是這樣的,因為AjaxMethod是我另外寫的類.跟Page是分開的,所以我這樣寫
    上面的方法我試過啦,不行
    煩麻你再看看吧,非常感謝
      

  6.   

    Ajax.Utility.RegisterTypeForAjax(typeof(AjaxTest.AjaxMethod));
    调用时
    AjaxTest.AjaxMethod.GetCityList(city.value,get_city_Result_CallBack);