XmlHttp实现无刷新三联动下拉框,网上有很多这样的例子,但这个存在一个问题,不能回传,回传就会报错"回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。"
鉴于这种错误,本人找到了解决方案,可能还有问题,望各位"大虾"指出,谢谢......
![](https://www.cnblogs.com/images/cnblogs_com/puke/XmlHttpdropdownlist.jpg)
1.html代码
<HTML>
<HEAD>
<title>XmlHttp实现无刷新三联动下拉框</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<INPUT style="Z-INDEX: 102; LEFT: 448px; WIDTH: 56px; POSITION: absolute; TOP: 8px; HEIGHT: 24px"
onclick="getData();" type="button" value="test">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 8px" runat="server"
Width="424px"></asp:TextBox>
<asp:DropDownList id="DropDownList2" runat="server" style="Z-INDEX: 103; LEFT: 176px; POSITION: absolute; TOP: 48px"></asp:DropDownList>
<asp:DropDownList id="DropDownList1" runat="server" style="Z-INDEX: 104; LEFT: 16px; POSITION: absolute; TOP: 48px"></asp:DropDownList>
<asp:DropDownList id="DropDownList3" runat="server" style="Z-INDEX: 105; LEFT: 296px; POSITION: absolute; TOP: 48px"></asp:DropDownList>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
<SCRIPT LANGUAGE="JavaScript">![](https://www.cnblogs.com/Images/dot.gif)
<!--
//以XML求取DropDownList2的数据
function XmlPost2(obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var svalue = obj.value;
var webFileUrl = "?provinceid=" + svalue;
var result = "";
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send("");
result = xmlHttp.responseText;
if(result != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
document.all("DropDownList2").length=0;
var piArray = result.split(",");
for(var i=0;i<piArray.length;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var ary1 = piArray[i].toString().split("|");
document.all("DropDownList2").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert(result);
}
}
//以XML求取DropDownList3的数据
function XmlPost3(obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var svalue = obj.value;
var webFileUrl = "?cityid=" + svalue;
var result = "";
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send("");
result = xmlHttp.responseText;
if(result != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
document.all("DropDownList3").length=0;
var piArray = result.split(",");
for(var i=0;i<piArray.length;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var ary1 = piArray[i].toString().split("|");
document.all("DropDownList3").options.add(new Option(ary1[1].toString(),ary1[0].toString()));
}
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
alert(result);
}
}
function getData()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
document.getElementById("<%=TextBox1.ClientID%>").innerText="省:"+pValue+"|"+pText+"市:"+cValue+"|"+cText+"区:"+aValue+"|"+aText;
}
//-->
</SCRIPT>
</form>
</body>
</HTML>
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2.cs代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace WebApplication1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList DropDownList3;
protected System.Web.UI.WebControls.TextBox TextBox1;
public static string ConnectionString=System.Configuration .ConfigurationSettings .AppSettings["ConnectionString"];
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
GetDataSet#region GetDataSet
public static DataSet GetDataSet(string sql)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlDataAdapter sda =new SqlDataAdapter(sql,ConnectionString);
DataSet ds=new DataSet();
sda.Fill(ds);
return ds;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
property#region property
private string provinceid
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(ViewState["provinceid"]!=null && ViewState["provinceid"].ToString()!="")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return ViewState["provinceid"].ToString();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(Request["provinceid"]!=null && Request["provinceid"].ToString()!="")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Request["provinceid"];
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
}
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ViewState["provinceid"]=value;
}
}
private string cityid
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(ViewState["cityid"]!=null && ViewState["cityid"].ToString()!="")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return ViewState["cityid"].ToString();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(Request["cityid"]!=null && Request["cityid"].ToString()!="")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return Request["cityid"];
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return "";
}
}
}
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
ViewState["provinceid"]=value;
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Page_Load#region Page_Load
private void Page_Load(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(!this.IsPostBack)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.down1_bind();
this.DropDownList1.Attributes.Add("onchange","XmlPost2(this);");
this.DropDownList2.Attributes.Add("onchange","XmlPost3(this);");
}
if(provinceid != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.down2_bind(provinceid);
}
if(cityid != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.down3_bind(cityid);
}
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
down2_bind#region down2_bind
private void down2_bind(string id)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string mystr = "";
string sql = "select cityID,city from city where father = '" + id + "'";
DataSet ds = GetDataSet(sql);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(ds.Tables[0].Rows.Count != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mystr += "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
down3_bind#region down3_bind
private void down3_bind(string id)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string mystr = "";
string sql = "select areaID,area from area where father = '" + id + "'";
DataSet ds = GetDataSet(sql);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if(ds.Tables[0].Rows.Count != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
mystr += "," + ds.Tables[0].Rows[i][0].ToString() + "|" + ds.Tables[0].Rows[i][1].ToString();
}
mystr = mystr.Substring(1);
}
this.Response.Write(mystr);
this.Response.End();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
down1_bind#region down1_bind
private void down1_bind()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string sql = "select provinceID,province from province";
DataSet ds = GetDataSet(sql);
this.DropDownList1.DataSource = ds;
this.DropDownList1.DataValueField = "provinceID";
this.DropDownList1.DataTextField = "province";
this.DropDownList1.DataBind();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
Web Form Designer generated code#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Load += new System.EventHandler(this.Page_Load);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
#endregion
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/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" onchange="changelocation(DropDownList3.options[DropDownList3.selectedIndex].value)">
</asp:DropDownList>
市
<select name="DDLcity" id="DDLcity" runat="server">
<option value="0">=请选择=</option>
</select>
然后在 后台 cs代码里用 Request.Form["DDLcity"]; 来接收城市的值。