se the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation

你。 提交于 2019-12-22 05:13:38

问题


guys, I have a usercontrol in my asp.net 3.5 application and I am passing some plain text on button click event. button is situated in the usercontrol. but when I fire the event, I am getting the following error;

Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.

when I set the EnableEventValidation="false" to web form page like blow. it fires the event;

<%@ Page EnableEventValidation="false" %>

but I am thinking that it shouldn't be a good idea to set that false. So, what is the alternative here? there error saying that 'use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.' but where am I gonna register that thing? thanks !

Also, I am using some AjaxControlToolkit controls inside my usercontrol and some jquery stuff.


回答1:


The problem with ASP.NET 2.0 event validation is that it is all or nothing and a bit heavy handed. I read an article that explains what you should do here.

Basically you need to register the child controls of the user control with the event validation engine.

C#
protected override void Render(HtmlTextWriter writer)
{
    // Register controls for event validation
    foreach (Control c in this.Controls)
    {
        this.Page.ClientScript.RegisterForEventValidation(
                c.UniqueID.ToString()
        );
    }
    base.Render(writer);
}

VB
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

    For Each aControl As Control In Me.Controls
        Me.Page.ClientScript.RegisterForEventValidation(aControl.UniqueID.ToString)
    Next aControl

    MyBase.Render(writer)
End Sub

Now this may not solve your problem because I am not sure how you are returning the string. Another article explains that the event validation is a combination of the control's unique ID and all the values it can pass back. If you are using the command value of a button to pass the text you may have trouble with this task.

If the event that is causing the issue is the click, then registering the controls of the user control should do the trick.




回答2:


try

if (!Page.IsPostBack)

before load and bind data in datagrid




回答3:


In my situation I was trying to update a GridViews datasource on page_load and I forgot to check if the request was a postback, thus my source was trying to change and it threw this error. Once I did the check for post back it worked fine. Hope this helps someone in the future.

    if (Page.IsPostBack == false)
    {
        this.sqlObj = new SqlServer(ConfigurationManager.ConnectionStrings["PdfReceiverConnectionString"].ToString());
        this.populateDataGrid();
    }



回答4:


You can leave it enabled then register your control for event validation. Add the following call in the PreRender or Render page life cycle then your control should work without having to turn off eventValidation:

  Page.ClientScript.RegisterForEventValidation(this.UniqueID);



回答5:


Check your HTML, nested from Tags will also cause that



来源:https://stackoverflow.com/questions/5764875/se-the-clientscriptmanager-registerforeventvalidation-method-in-order-to-registe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!