ASP.NET2.0 的关于脚本回调的问题!!!~~~~!!!

Kidwind 2006-07-26 05:56:31
我写一个脚本回调的关联下拉列表控件的例子,但在提交时出现了以下错误

回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。

按照提示我在代码中 override Render 方法加入了 ClientScriptManager.RegisterForEventValidation 如下:

protected override void Render(HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation(ChildOrganizations.ID);
base.Render(writer);
}

但问题依旧存在,请问我应该如何处理这样的问题呢????
完整代码如下:


Default.aspx
---------------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>


<!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>Client CallBack</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:DropDownList ID="ParentOrganizations" runat="server" />
<asp:DropDownList ID="ChildOrganizations" runat="Server" />
<asp:Button ID="BtSubmit" runat="server" Text="确定" />

<script language="javascript">
function ClientCallback(result, context){
var childOrganizations = document.forms[0].elements[
'<%=ChildOrganizations.UniqueID%>'];
if (!childOrganizations){
return;
}
childOrganizations.length = 0;
if (!result){
return;
}

var rows = result.split('|');
for (var i = 0; i < rows.length; ++i){
var values = rows[i].split('^');
var option = document.createElement("OPTION");
option.value = values[0];
option.innerHTML = values[1];
childOrganizations.appendChild(option);
}
}
</script>

function ClientCallbackError(result, context){ alert(result); }
</form>
</body>
</html>


Default.aspx.cs
---------------------------------------------------------------------------
using System;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
{
private const string connectionString = @"Database=Attorney;Server=(local);Uid=sa;Pwd=digital;Integrated Security=false;";

private void Page_Load(object source, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = GetOrganizations();
DataView dv = dt.DefaultView;
dv.RowFilter = "PDicID IS NULL";
ParentOrganizations.DataSource = dv;
ParentOrganizations.DataTextField = "Name";
ParentOrganizations.DataValueField = "DicID";
ParentOrganizations.DataBind();
ParentOrganizations.Items.Insert(0, new ListItem("Select", "0"));

ParentOrganizations.Attributes.Add("onchange", "GetChildren(this.options[this.selectedIndex].value, 'ddl');");
}

string callBack = Page.ClientScript.GetCallbackEventReference(this, "arg", "ClientCallback", "context");
string clientFunction = "function GetChildren(arg, context){ " + callBack + "; }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "GetChildren", clientFunction, true);
}

protected override void Render(HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation(ChildOrganizations.ID);
base.Render(writer);
}

private string _callbackResult = "";
public void RaiseCallbackEvent(string eventArgument)
{
int parentId;
if (Int32.TryParse(eventArgument, out parentId))
{
DataTable dt = GetOrganizations();
DataView dv = dt.DefaultView;
dv.RowFilter = "PDicID = " + parentId.ToString();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dv.Count; ++i)
{
sb.Append(dv[i]["DicID"]);
sb.Append("^");
sb.Append(dv[i]["Name"]);
sb.Append("|");
}
_callbackResult = sb.ToString();
}
}

public string GetCallbackResult()
{
return _callbackResult;
}

private DataTable GetOrganizations()
{
DataTable dt = (DataTable)Cache["Organizations"];
if (dt == null)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("SELECT * FROM Dictionary WHERE ClassID = 1", connection);
command.CommandType = CommandType.Text;

SqlDataAdapter da = new SqlDataAdapter(command);
dt = new DataTable();
try
{
connection.Open();
da.Fill(dt);
Cache.Insert("Organizations", dt, null, DateTime.Now.AddHours(6), TimeSpan.Zero);
}
finally
{
connection.Dispose();
command.Dispose();
da.Dispose();
}
}
return dt;
}
}
...全文
245 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
karykwan 2006-07-29
  • 打赏
  • 举报
回复
改了之后要重新编译,生成工程一下.再浏览
nZAI 2006-07-26
  • 打赏
  • 举报
回复
这里有个类似例子http://nzai.cnblogs.com/
hanchi8008 2006-07-26
  • 打赏
  • 举报
回复
沙发 再来跟烟~~~~~~~

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧