Ajax实现无刷新三联动下拉框

1.html代码
None.gif < HTML >
None.gif    
< HEAD >
None.gif        
< title > Ajax实现无刷新三联动下拉框 </ title >
None.gif        
< meta  content ="Microsoft Visual Studio .NET 7.1"  name ="GENERATOR" >
None.gif        
< meta  content ="C#"  name ="CODE_LANGUAGE" >
None.gif        
< meta  content ="JavaScript"  name ="vs_defaultClientScript" >
None.gif        
< meta  content ="http://schemas.microsoft.com/intellisense/ie5"  name ="vs_targetSchema" >
ExpandedBlockStart.gifContractedBlock.gif        
< SCRIPT  language ="javascript" > dot.gif             
InBlock.gif            
//城市------------------------------
InBlock.gif
            function cityResult() 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
var city=document.getElementById("DropDownList1");
InBlock.gif                AjaxMethod.GetCityList(city.value,get_city_Result_CallBack);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
InBlock.gif            
function get_city_Result_CallBack(response)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (response.value != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    
//debugger;
InBlock.gif
                    document.all("DropDownList2").length=0;                
InBlock.gif                
var ds = response.value;
InBlock.gif                    
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{                    
InBlock.gif                        
for(var i=0; i<ds.Tables[0].Rows.length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
var name=ds.Tables[0].Rows[i].city;
InBlock.gif                      
var id=ds.Tables[0].Rows[i].cityID;
InBlock.gif                      document.all(
"DropDownList2").options.add(new Option(name,id));
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }
                
InBlock.gif                
return
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//市区----------------------------------------
InBlock.gif
            function areaResult() 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                
var area=document.getElementById("DropDownList2");
InBlock.gif                AjaxMethod.GetAreaList(area.value,get_area_Result_CallBack);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
function get_area_Result_CallBack(response)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (response.value != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{                    
InBlock.gif                    document.all(
"DropDownList3").length=0;                
InBlock.gif                
var ds = response.value;
InBlock.gif                    
if(ds != null && typeof(ds) == "object" && ds.Tables != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{                    
InBlock.gif                        
for(var i=0; i<ds.Tables[0].Rows.length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                      
var name=ds.Tables[0].Rows[i].area;
InBlock.gif                      
var id=ds.Tables[0].Rows[i].areaID;
InBlock.gif                      document.all(
"DropDownList3").options.add(new Option(name,id));
ExpandedSubBlockEnd.gif                    }
                
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
return
ExpandedSubBlockEnd.gif            }

InBlock.gif            
function getData()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
var province=document.getElementById("DropDownList1");
InBlock.gif                
var pindex = province.selectedIndex;
InBlock.gif                
var pValue = province.options[pindex].value;
InBlock.gif                
var pText  = province.options[pindex].text;
InBlock.gif                
InBlock.gif                
var city=document.getElementById("DropDownList2");
InBlock.gif                
var cindex = city.selectedIndex;
InBlock.gif                
var cValue = city.options[cindex].value;
InBlock.gif                
var cText  = city.options[cindex].text;
InBlock.gif                
InBlock.gif                
var area=document.getElementById("DropDownList3");
InBlock.gif                
var aindex = area.selectedIndex;
InBlock.gif                
var aValue = area.options[aindex].value;
InBlock.gif                
var aText  = area.options[aindex].text;
InBlock.gif                
InBlock.gif                
var txt=document.getElementById("TextBox1");                                
InBlock.gif
InBlock.gif                document.getElementById(
"<%=TextBox1.ClientID%>").innerText="省:"+pValue+"|"+pText+"市:"+cValue+"|"+cText+"区:"+aValue+"|"+aText;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        
</ SCRIPT >
None.gif    
</ HEAD >
None.gif    
< body  ms_positioning ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< TABLE  id ="Table1"  style ="Z-INDEX: 101; LEFT: 96px; POSITION: absolute; TOP: 32px"  cellSpacing ="1"
None.gif                cellPadding
="1"  width ="300"  border ="1"  bgColor ="#ccff66" >
None.gif                
< TR >
None.gif                    
< TD > 省市 </ TD >
None.gif                    
< TD >< asp:dropdownlist  id ="DropDownList1"  runat ="server" ></ asp:dropdownlist ></ TD >
None.gif                
</ TR >
None.gif                
< TR >
None.gif                    
< TD > 城市 </ TD >
None.gif                    
< TD >< asp:dropdownlist  id ="DropDownList2"  runat ="server" ></ asp:dropdownlist ></ TD >
None.gif                
</ TR >
None.gif                
< TR >
None.gif                    
< TD > 市区 </ TD >
None.gif                    
< TD >< asp:dropdownlist  id ="DropDownList3"  runat ="server" ></ asp:dropdownlist ></ TD >
None.gif                
</ TR >
None.gif            
</ TABLE >
None.gif            
< asp:TextBox  id ="TextBox1"  style ="Z-INDEX: 102; LEFT: 416px; POSITION: absolute; TOP: 48px"  runat ="server"
None.gif                Width
="424px" ></ asp:TextBox >< INPUT  style ="Z-INDEX: 103; LEFT: 456px; WIDTH: 56px; POSITION: absolute; TOP: 96px; HEIGHT: 24px"
None.gif                type
="button"  value ="test"  onclick ="getData();" >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >

2.cs代码
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
namespace  AjaxTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for WebForm1.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class WebForm1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList DropDownList1;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList DropDownList2;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif        
protected System.Web.UI.WebControls.DropDownList DropDownList3;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            Ajax.Utility.RegisterTypeForAjax(
typeof(AjaxMethod));
InBlock.gif            
if(!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.DropDownList1.DataSource=AjaxMethod.GetProvinceList();
InBlock.gif                
this.DropDownList1.DataTextField="province";
InBlock.gif                
this.DropDownList1.DataValueField="provinceID";
InBlock.gif                
this.DropDownList1.DataBind();
InBlock.gif                
InBlock.gif                
this.DropDownList1.Attributes.Add("onclick","cityResult();");
InBlock.gif                
this.DropDownList2.Attributes.Add("onclick","areaResult();");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form Designer generated code#region Web Form Designer generated code
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Required method for Designer support - do not modify
InBlock.gif        
/// the contents of this method with the code editor.
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion
        
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

.AjaxMethod
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
namespace  AjaxTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Summary description for AjaxMethod.
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class AjaxMethod
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetProvinceList#region GetProvinceList
InBlock.gif        
public static DataSet GetProvinceList()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sql="select * from province";
InBlock.gif            
return GetDataSet(sql);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetCityList#region GetCityList
InBlock.gif        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
InBlock.gif        
public  DataSet GetCityList(int provinceid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sql="select * from city where father="+provinceid;
InBlock.gif            
return GetDataSet(sql);            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetAreaList#region GetAreaList
InBlock.gif        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
InBlock.gif        
public  DataSet GetAreaList(int cityid)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sql="select * from area where father="+cityid;
InBlock.gif            
return GetDataSet(sql);            
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif    
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetDataSet#region GetDataSet
InBlock.gif        
public static DataSet GetDataSet(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];
InBlock.gif            SqlDataAdapter    sda 
=new SqlDataAdapter(sql,ConnectionString);
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            sda.Fill(ds);
InBlock.gif            
return ds;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

4.web.config
None.gif < httpHandlers >
None.gif            
< add  verb ="POST,GET"  path ="ajax/*.ashx"  type ="Ajax.PageHandlerFactory, Ajax"   />
None.gif    
</ httpHandlers >

5.ajax.dll下载 /Files/singlepine/Ajax.rar
6.真实数据库下载 area1.rar
7.源代码下载 AjaxTest.rar

转载于:https://www.cnblogs.com/Actuping/archive/2006/12/09/587509.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值