错过提示如下:
“/HFA”应用程序中的服务器错误。
--------------------------------------------------------------------------------回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentException: 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。源错误: 执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。 堆栈跟踪:
[ArgumentException: 回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。]
System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +8620921
System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +72
System.Web.UI.WebControls.DropDownList.LoadPostData(String postDataKey, NameValueCollection postCollection) +53
System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection) +13
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +346
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1743
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:2.0.50727.3053; ASP.NET 版本:2.0.50727.3053 DropDownList.aspx 文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownList.aspx.cs" Inherits="Execute_DropDownList" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
<!--
// 以XML求取数据
// 参数:
// obj:当前动作的下拉框
// fullName:要填充的下拉框
function XmlPost(obj, fullNme) {
// 求取当前下拉框的值
var svalue = obj.value;
// 定义要填充的对象
var fullObj = document.all(fullNme);
//定义取值地址
var webFileUrl = "DropDownList.aspx?Customer_Id=" + svalue;
// 定义返回值
var result = ""; // 取始取值过程
var xmlHttp = new ActiveXObject("MSXML2.XMLHTTP");
xmlHttp.open("POST", webFileUrl, false);
xmlHttp.send("");
result = xmlHttp.responseText; // 如果有取到值, 则根据格式进行拆分
// 注意如果选择了第一行"请选择"空行时,是取不取值的, 因此多加了一个条件
if (result != "" && svalue != "") {
// 先清空原有的值
fullObj.length = 0;
// 拆分值成数组
var piArray = result.split(","); // 循环数组
for (var i = 0; i < piArray.length; i++) {
// 再拆分成ID及名称
var ary1 = piArray[i].toString().split("|");
// 逐一添加项
fullObj.options.add(new Option(ary1[1].toString(), ary1[0].toString()));
}
}
else {
// 如果没有取到值, 则清空要填充的下拉框的值
fullObj.length = 0;
fullObj.options.add(new Option("---请选择---", "-1"));
}
}
//-->
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
Customer:<asp:DropDownList ID="ddl_Customer" runat="server"></asp:DropDownList>
Contact:<asp:DropDownList ID="ddl_Contact" runat="server"></asp:DropDownList>
<asp:Button ID="btn_Submit" runat="server" Text="Submit" />
</div>
</form>
</body>
</html>
DropDownList.aspx.cs 文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;public partial class Execute_DropDownList : System.Web.UI.Page
{
/// <summary>
/// 数据库连接字符串
/// </summary>
private string connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; private void xmlBind(string customer_Id)
{
string cmdText = "SELECT [Id], SG1 FROM Customer_Contact WHERE Customer_Id = @Customer_Id ORDER BY SG1";
DataTable dt_Customer_Contact = new DataTable("Customer_Contact");
DataHelper.SqlHelper.Fill(dt_Customer_Contact, connectionString, CommandType.Text, cmdText, new SqlParameter("@Customer_Id", customer_Id));
// 将取得的值开成"ID|名称,ID|名称,..."这样的形式
string str = "";
if (dt_Customer_Contact.Rows.Count > 0)
{
for (int i = 0; i < dt_Customer_Contact.Rows.Count; i++)
{
str += string.Format(",{0}|{1}", dt_Customer_Contact.Rows[i]["Id"].ToString().Trim(), dt_Customer_Contact.Rows[i]["SG1"].ToString().Trim());
}
str = str.Substring(1);
} // 输出页面
Response.Write(str);
Response.End();
} private void bind_Customer()
{
// 显示所有主类别
string cmdText = "SELECT [Id], [Name] FROM Customer ORDER BY [Name]";
DataTable dt_Customer = new DataTable("Customer");
DataHelper.SqlHelper.Fill(dt_Customer, connectionString, CommandType.Text, cmdText); ddl_Customer.DataSource = dt_Customer;
ddl_Customer.DataValueField = "Id";
ddl_Customer.DataTextField = "Name";
ddl_Customer.DataBind(); ddl_Customer.Items.Insert(0, new ListItem("---请选择---", "-1")); ListItem item = ddl_Customer.Items.FindByValue("1");
if (item != null)
{
item.Selected = true;
} ddl_Customer.Attributes.Add("onchange","XmlPost(this,'" + ddl_Contact.ClientID + "');");
} private void bind_Contact()
{
string cmdText = "SELECT [Id], SG1 FROM Customer_Contact WHERE Customer_Id = '1' ORDER BY SG1";
DataTable dt_Customer_Contact = new DataTable("Custome_Contact");
DataHelper.SqlHelper.Fill(dt_Customer_Contact, connectionString, CommandType.Text, cmdText); ddl_Contact.DataSource = dt_Customer_Contact;
ddl_Contact.DataValueField = "Id";
ddl_Contact.DataTextField = "SG1";
ddl_Contact.DataBind(); ddl_Contact.Items.Insert(0, new ListItem("---请选择---", "-1"));
} protected void Page_Load(object sender, EventArgs e)
{
string customer_Id = Request.QueryString["Customer_Id"];
if (customer_Id + "a" != "a")
{
xmlBind(customer_Id);
} if (!IsPostBack)
{
bind_Customer();
bind_Contact();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
string customer_Id = Request.QueryString["Customer_Id"];
if (customer_Id + "a" != "a")
{
xmlBind(customer_Id);
} if (!IsPostBack)
{
bind_Customer();
bind_Contact();
}
}
换成 protected void Page_Load(object sender, EventArgs e)
{
string customer_Id = Request.QueryString["Customer_Id"];
if (!string.IsNullOrEmpty(customer_Id))
{
xmlBind(customer_Id);
} if (!IsPostBack)
{
bind_Customer();
bind_Contact();
}
}
如果我这个Submit按是实现"保存"功能呢? 那该怎么处理?
if (!string.IsNullOrEmpty(customer_Id))
{
xmlBind(customer_Id);
}受益....
是的, 用一个LinkButton或Button的Asp.net控件, 在Click事件中写代码, 读到DropDownList中SelectedValue值,并向数据库中保存数据.
http://xljpeter.blog.hexun.com/9622483_d.html
这样写function setValue()
{
var customerSel=document.getElementById('<%= ddl_Customer.ClientID%>');
var customerValue=customerSel.value;
var customerText=customerSel.options[customerSel.selectedIndex].text;
var contactSel=document.getElementById('<%= ddl_Contact.ClientID%>');
var contactValue=contactSel.value;
var contactText=contactSel.options[contactSel.selectedIndex].text;
document.getElementById('<%= hidCustomer.ClientID%>').value=customerText+','+customerValue;
document.getElementById('<%= hidContact.ClientID%>').value=contactText+','+contactValue;
}
<asp:Button ID="btn_Submit" runat="server" Text="Submit" OnClientClick="setValue();" OnClick="btn_Submit_Click" />
<asp:HiddenField ID="hidCustomer" runat="server" />
<asp:HiddenField ID="hidContact" runat="server" />
protected void btn_Submit_Click(object sender, EventArgs e)
{
string[] customer = hidCustomer.Value.Split(',');
string[] contact = hidContact.Value.Split(',');
//customer[0] 为 ddl_Customer 的选择文本,customer[1] 为 ddl_Customer 的选择value
//contact 同上
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownList.aspx.cs" Inherits="Execute_DropDownList" %>
换成<%@ Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeFile="DropDownList.aspx.cs" Inherits="Execute_DropDownList" %>
http://www.cnblogs.com/dongyongjing/archive/2007/03/07/666427.html
需要将DropDownList控件改为HTML Select控件。
参考
在使用过程中通过AJAX修改了服务器端控件内容,显示是变了,但实际上后面回发到服务器端时,服务器不认为它变了,跟它原来的样子变了,服务器就报错了。如果都用AJAX来完成,就不会有这样的问题了。