zoukankan      html  css  js  c++  java
  • [转]Invalid postback or callback argument

    原文:
    http://weblogs.asp.net/davidfowler/archive/2009/03/09/invalid-postback-or-callback-argument-in-the-datacontrols.aspx


    Invalid postback or callback argument

    I'm sure many of you have seen this error message when developing your web application:

    Server Error in '/' Application.
    --------------------------------------------------------------------------------

    Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> 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.

    I'm going to discuss this in the context of the data controls. This happens when a control that isn't registered for event validation causes a postback, but surely that can't be the case.. right?

    Let's look at a small repro:

    Markup:

    <asp:GridView ID="GridView1" runat="server">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:Button runat="server" Text="Button" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

    Code behind:

    public partial class _Default : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            GridView1.DataSource = Enumerable.Range(0, 5);
            GridView1.DataBind();
        }
    }

    Now click on the button and see the dreaded error message. Why does this happen? EventValidation was added in ASP.NET to ensure that controls causing the postback came from the same page being rendered. Take a look at __EVENTVALIDATION hidden field on the page. It is a serialized version of all of the controls registered for postbacks(read more here). You might be wondering how they got in there and why is the button inside of a GridView a special case. It's not a special case, in fact, Button registers itself with the current page.

    The reason this happens is because we rebind the data control in Page_Load every time which means that we will lose all of the posted data and viewstate. As a result, the ID of the button is different and when the event is validated there will be no matching unique id and hence event validation will fail. We are acutally raising an event for a button that is no longer in the control tree.

    You can work around this by wrapping that code in if (!IsPostBack). This is a good proof of why you should use DataSource controls.

    Hope this helps

    Published Monday, March 09, 2009 12:54 AM by davidfowl Filed under: ASP.NET, DataControls
     

  • 相关阅读:
    topcoder srm 633 div1
    HDU 4997 Biconnected (状态压缩DP)
    HDU 5013 City Tour
    BZOJ 3672 [Noi2014]购票 (熟练剖分+凸壳维护)
    BZOJ 1488: [HNOI2009]图的同构 polay
    BZOJ 1565 植物大战僵尸(最大权闭合图)
    iOS加载程序视图的方式
    内存管理2(主讲MRR)
    内存管理1
    排序算法之希尔排序
  • 原文地址:https://www.cnblogs.com/LeoWong/p/2199995.html
Copyright © 2011-2022 走看看