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

 Ajax使用初步,实现步骤
1.把Ajax.dll copy到应用系统bin目录下,然后在工程引用中引用Ajax.dll,如果没有的话可以下载Ajax.dll
2.配置web.config,添加如下信息

None.gif < httpHandlers >
None.gif  
< add verb = " POST,GET "  path = " ajax/*.ashx "  type = " Ajax.PageHandlerFactory, Ajax "   />
None.gif
</ httpHandlers >
3.假设应用系统叫Document,目录格式为Pages\AjaxTest
None.gif // [Ajax.AjaxMethod]
None.gif
[Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
None.gif
public   static   string  GetNIVNumber( string  str)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
return str;
ExpandedBlockEnd.gif}
4.在Pages\AjaxTest下建立页面WebForm1.aspx
5.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 " >
None.gif        
< SCRIPT language = " javascript " >             
None.gif            
// 城市------------------------------
None.gif
            function cityResult() 
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif
InBlock.gif                var city
=document.getElementById("DropDownList1");
InBlock.gif                AjaxMethod.GetCityList(city.value,get_city_Result_CallBack);
ExpandedBlockEnd.gif            }

None.gif            
None.gif            function get_city_Result_CallBack(response)
ExpandedBlockStart.gifContractedBlock.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
ExpandedBlockEnd.gif            }

None.gif            
// 市区----------------------------------------
None.gif
            function areaResult() 
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif
InBlock.gif                var area
=document.getElementById("DropDownList2");
InBlock.gif                AjaxMethod.GetAreaList(area.value,get_area_Result_CallBack);
ExpandedBlockEnd.gif            }

None.gif            function get_area_Result_CallBack(response)
ExpandedBlockStart.gifContractedBlock.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
ExpandedBlockEnd.gif            }

None.gif            function getData()
ExpandedBlockStart.gifContractedBlock.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;
ExpandedBlockEnd.gif            }

None.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 >
6. cs代码
None.gif public   class  WebForm1 : System.Web.UI.Page
ExpandedBlockStart.gifContractedBlock.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{  //注册,AjaxMethod.cs方法所在的命名空间
InBlock.gif            Ajax.Utility.RegisterTypeForAjax(
typeof(AjaxMethod));
InBlock.gif            
if(!Page.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.DropDownList1.DataSource=AjaxMethod.GetPovinceList();
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
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
        
ExpandedBlockEnd.gif    }
7.在Pages\AjaxTest下建立cs文件AjaxMethod.cs(业务逻辑类),添加如下方法
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        GetPovinceList
GetPovinceList#region GetPovinceList
InBlock.gif        
public static DataSet GetPovinceList()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string sql="select * from povince";
InBlock.gif            
return GetDataSet(sql);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

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

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        GetAreaList
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
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}
8.  Ajax实现无刷新三级连动完整源代码(附数据库)

转载于:https://www.cnblogs.com/zhouxiaxue/archive/2006/05/11/396906.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值