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

1.html代码
None.gif < HTML >
None.gif    
< HEAD >
None.gif        
< title > XmlHttp实现无刷新三联动下拉框 </ title >
None.gif        
< meta  name ="GENERATOR"  Content ="Microsoft Visual Studio .NET 7.1" >
None.gif        
< meta  name ="CODE_LANGUAGE"  Content ="C#" >
None.gif        
< meta  name ="vs_defaultClientScript"  content ="JavaScript" >
None.gif        
< meta  name ="vs_targetSchema"  content ="http://schemas.microsoft.com/intellisense/ie5" >
None.gif    
</ HEAD >
None.gif    
< body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< INPUT  style ="Z-INDEX: 102; LEFT: 448px; WIDTH: 56px; POSITION: absolute; TOP: 8px; HEIGHT: 24px"
None.gif                onclick
="getData();"  type ="button"  value ="test" >
None.gif            
< asp:TextBox  id ="TextBox1"  style ="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 8px"  runat ="server"
None.gif                Width
="424px" ></ asp:TextBox >
None.gif            
< asp:DropDownList  id ="DropDownList2"  runat ="server"  style ="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 48px" ></ asp:DropDownList >
None.gif            
< asp:DropDownList  id ="DropDownList1"  runat ="server"  style ="Z-INDEX: 104; LEFT: 16px; POSITION: absolute; TOP: 48px" ></ asp:DropDownList >
None.gif            
< asp:DropDownList  id ="DropDownList3"  runat ="server"  style ="Z-INDEX: 105; LEFT: 296px; POSITION: absolute; TOP: 48px" ></ asp:DropDownList >
ExpandedBlockStart.gifContractedBlock.gif            
< SCRIPT  LANGUAGE ="JavaScript" > dot.gif
InBlock.gif            
<!--
InBlock.gif                
//以XML求取DropDownList2的数据
InBlock.gif
                function XmlPost2(obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                  
var svalue = obj.value;
InBlock.gif                  
var webFileUrl = "?provinceid=" + svalue;
InBlock.gif                  
var result = "";
InBlock.gif                  
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
InBlock.gif                  xmlHttp.open(
"POST", webFileUrl, false);
InBlock.gif                  xmlHttp.send(
"");
InBlock.gif                  result 
= xmlHttp.responseText;
InBlock.gif                  
InBlock.gif                  
if(result != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{
InBlock.gif                    document.all(
"DropDownList2").length=0;
InBlock.gif                    
var piArray = result.split(",");
InBlock.gif                    
for(var i=0;i<piArray.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                      
var ary1 = piArray[i].toString().split("|");
InBlock.gif                      document.all(
"DropDownList2").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                  }

InBlock.gif                  
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{
InBlock.gif                    alert(result);
ExpandedSubBlockEnd.gif                  }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
//以XML求取DropDownList3的数据
InBlock.gif
                function XmlPost3(obj)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                  
var svalue = obj.value;
InBlock.gif                  
var webFileUrl = "?cityid=" + svalue;
InBlock.gif                  
var result = "";
InBlock.gif                  
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
InBlock.gif                  xmlHttp.open(
"POST", webFileUrl, false);
InBlock.gif                  xmlHttp.send(
"");
InBlock.gif                  result 
= xmlHttp.responseText;
InBlock.gif                  
InBlock.gif                  
if(result != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{
InBlock.gif                    document.all(
"DropDownList3").length=0;
InBlock.gif                    
var piArray = result.split(",");
InBlock.gif                    
for(var i=0;i<piArray.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                      
var ary1 = piArray[i].toString().split("|");
InBlock.gif                      document.all(
"DropDownList3").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                  }

InBlock.gif                  
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                  
dot.gif{
InBlock.gif                    alert(result);
ExpandedSubBlockEnd.gif                  }

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                }

InBlock.gif            
//-->
ExpandedBlockEnd.gif
            
</ SCRIPT >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >
None.gif
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
using  System.Data.SqlClient;
None.gif
namespace  WebApplication1
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    
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.DropDownList DropDownList3;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif    
InBlock.gif        
public static string ConnectionString=System.Configuration .ConfigurationSettings .AppSettings["ConnectionString"];
InBlock.gif    
ContractedSubBlock.gifExpandedSubBlockStart.gif        
GetDataSet#region GetDataSet
InBlock.gif        
public static DataSet GetDataSet(string sql)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
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

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
property#region property
InBlock.gif        
private string provinceid
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(ViewState["provinceid"]!=null && ViewState["provinceid"].ToString()!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return ViewState["provinceid"].ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(Request["provinceid"]!=null && Request["provinceid"].ToString()!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return Request["provinceid"];
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return "";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"provinceid"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private string cityid
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(ViewState["cityid"]!=null && ViewState["cityid"].ToString()!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return ViewState["cityid"].ToString();
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
if(Request["cityid"]!=null && Request["cityid"].ToString()!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return Request["cityid"];
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
return "";
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                ViewState[
"provinceid"]=value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Page_Load#region Page_Load
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!this.IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            
this.down1_bind();
InBlock.gif                
this.DropDownList1.Attributes.Add("onchange","XmlPost2(this);");
InBlock.gif                
this.DropDownList2.Attributes.Add("onchange","XmlPost3(this);");
ExpandedSubBlockEnd.gif          }

InBlock.gif          
if(provinceid != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            
this.down2_bind(provinceid);
ExpandedSubBlockEnd.gif          }

InBlock.gif            
if(cityid != "")
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            
this.down3_bind(cityid);
ExpandedSubBlockEnd.gif          }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
down2_bind#region down2_bind
InBlock.gif        
private void down2_bind(string id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
string mystr = "";
InBlock.gif          
string sql = "select cityID,city from city where father = '" + id + "'";
InBlock.gif          DataSet ds 
= GetDataSet(sql);
InBlock.gif
InBlock.gif          
if(ds.Tables[0].Rows.Count != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif              mystr 
+= "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            mystr 
= mystr.Substring(1);
ExpandedSubBlockEnd.gif          }

InBlock.gif          
this.Response.Write(mystr);
InBlock.gif          
this.Response.End();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
down3_bind#region down3_bind
InBlock.gif        
private void down3_bind(string id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
string mystr = "";
InBlock.gif          
string sql = "select areaID,area from area where father = '" + id + "'";
InBlock.gif          DataSet ds 
= GetDataSet(sql);
InBlock.gif
InBlock.gif          
if(ds.Tables[0].Rows.Count != 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif          
dot.gif{
InBlock.gif            
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif              mystr 
+= "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif            mystr 
= mystr.Substring(1);
ExpandedSubBlockEnd.gif          }

InBlock.gif          
this.Response.Write(mystr);
InBlock.gif          
this.Response.End();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif        
ContractedSubBlock.gifExpandedSubBlockStart.gif        
down1_bind#region down1_bind
InBlock.gif        
private void down1_bind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          
string sql = "select provinceID,province from province";
InBlock.gif          DataSet ds 
= GetDataSet(sql);
InBlock.gif          
this.DropDownList1.DataSource = ds;
InBlock.gif          
this.DropDownList1.DataValueField = "provinceID";
InBlock.gif          
this.DropDownList1.DataTextField = "province";
InBlock.gif          
this.DropDownList1.DataBind();          
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

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}

None.gif

3.数据库area1.rar
4.Ajax实现无刷新三联动下拉框

5.源代码下载XmlHttpselect.rar

6.Asp.Net 2.0 中 回发或回调参数无效 的解决方案
      回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。

目前为止遇到的出现情况有两种:
一是Form嵌套,一个页面只能有一个Form,仔细检查代码就可以解决。
二是在下拉菜单中使用javascript 来更改内容(如AJAX),常见于省 市 联动菜单,在事件回发时提示该错误。
该错误只有在  vs 2.0 上才会出现,如果把 enableEventValidation="true"  改成 enableEventValidation="false" 错误是不会出事了但是在代码中确读不到此项的值。
第一种情况的话不能在form 里嵌form
第二种情况的解决办法 就是把 控制改成 Html 的控制,控件带上 runat="server"
如:下面是我的一个解决办法
一个省份和城市的链动效果
<asp:DropDownList ID="DropDownList3" runat="server" οnchange="changelocation(DropDownList3.options[DropDownList3.selectedIndex].value)">
        </asp:DropDownList>
<select name="DDLcity"  id="DDLcity" runat="server">
       <option value="0">=请选择=</option>
     </select>
然后在 后台 cs代码里用          Request.Form["DDLcity"]; 来接收城市的值。

转载于:https://www.cnblogs.com/puke/archive/2007/05/26/760452.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值