现在的位置: 首页 > 综合 > 正文

回发或回调参数无效。在配置中使用 <%@ Page EnableEventValidation="true" %>

2012年02月21日 ⁄ 综合 ⁄ 共 4893字 ⁄ 字号 评论关闭
 
回发或回调参数无效。在配置中使用 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。

目前为止遇到的出现情况有两种:
一是Form嵌套,一个页面只能有一个Form,仔细检查代码就可以解决。
二是在下拉菜单中使用ajax,常见于省市联动菜单,可能是由于在aspx页面赋给了下拉菜单初始Item值,在事件回发时提示该错误,将下拉菜单初始Item值删除,在绑定事件中添加Item项。

后来我自己发现,问题还不是出在那儿.
原因是 DropDownList 控件的ListItem 的Value 属性 包含汉字.只要将Value 改为英文或数字的就行了.
网上还有一种解决办法:把 EnableEventValidation 属性设置为false;但是这样只是没有警告框而已.数据仍然无法传递给服务器,导致SelectedIndex 值一直为零.

最好在web.config中添加如下语句:
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="zh-CN" uiCulture="zh-CN"/>
结果搞定,现在贴到这里希望对大家能够有所帮助.

总结:

上网搜索了一下,可行的解决方法有:

1、在页面的<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 中添加 EnableEventValidation="false" 就可以了。(首先考虑的)

2、是Form嵌套,一个页面只能有一个Form,仔细检查代码就可以解决。

3、如果页面含有 DropDownList 或 ListBox这样的控件,可能以下原因造成:

    3.1 在下拉菜单中使用ajax,常见于省市联动菜单,可能是由于在aspx页面赋给了下拉菜单初始Item值,在事件回发时提示该错误,将下拉菜单初始Item值删除,在绑定事件中添加Item项。

    3.2 原因是 DropDownList 控件的ListItem 的Value 属性 包含汉字.只要将Value 改为英文或数字的就行了.最好在web.config中添加如下语句:
<globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="zh-CN" uiCulture="zh-CN"/>因为 POSTBACK 如果不采用 UTF-8 编码, JAVASCRIPT 会认为有问题。
只改 requestEncoding="utf-8" 就可以了,responseEncoding="utf-8" 不用


看来ajax.net对中文支持有限啊,还是因为要在Web.Config设置一下编码??????

给gridview添加上下移动功能

CREATE PROCEDURE [sp_trans_dept]
@now_id int,
@upside_id int
AS
declare @tmp_ordering int       --临时变量
declare @sqlstr varchar(1000) --sql语句
declare @table_name varchar(500)
declare @column_name varchar(500)
set @table_name='tb_dept'
set @column_name='deptID'
select @tmp_ordering=ordering from tb_dept where deptID=@now_id
set @sqlstr='
update '+@table_name+' set ordering = '+convert(varchar(50),@tmp_ordering)+'
where '+@column_name+'='+convert(varchar(50),@upside_id)
exec (@sqlstr)
GO
CREATE TRIGGER TRI_tb_dept ON dbo.tb_dept
FOR INSERT
AS
update dbo.tb_dept
set ordering=inserted.deptID
from inserted
where tb_dept.deptID=inserted.deptID
SQLOperation opera = new SQLOperation();
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "row_up")
        {
            if ((index - 1) >= 0) {
                int now_id = (int)GridView1.DataKeys[index].Value;
                int upside=(int)GridView1.DataKeys[index-1].Value;
                try
                {
                    opera.StoredProcedureOrdering("sp_trans_dept", "@now_id", "@upside_id", now_id, upside);
                    GridView1.DataBind();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            else
            {
                Alert("已经是首行,不能上移");
            }
        }
        else if (e.CommandName=="row_down")
        {
            int rowsCount = GridView1.Rows.Count;
            if ((index+1)<rowsCount)
            {
                int now_id = (int)GridView1.DataKeys[index].Value;
                int upside = (int)GridView1.DataKeys[index + 1].Value;
                try
                {
                    opera.StoredProcedureOrdering("sp_trans_dept", "@now_id", "@upside_id", now_id, upside);
                    GridView1.DataBind();
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            else
            {
                Alert("已经是尾行,不能下移");
            }
        }
    }
    #region   警告信息
    public void Alert(string message)
    {
        string outstr = "";
        outstr += "<script   type='text/javascript'>";
        outstr += "alert('" + message + "');";
        outstr += "</script>";
        Response.Write(outstr);
    }
    #endregion
<asp:ButtonField CommandName="row_up" Text="上移">
                  <ItemStyle Width="40px" />
              </asp:ButtonField>
              <asp:ButtonField CommandName="row_down" Text="下移">
                  <ItemStyle Width="40px" />
              </asp:ButtonField>
C#
public override void StoredProcedureOrdering(string sp_Name,string para_name1,string para_name2,int value_1,int value_2)
        {
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sp_Name, conn);
            if (conn.State.ToString() == "Closed")
                conn.Open();
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.Add(para_name1, SqlDbType.Int, 4).Value = value_1;
            cmd.Parameters.Add(para_name2, SqlDbType.Int, 4).Value = value_2;
           
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

抱歉!评论已关闭.