Invalid postback or callback argument. Event validation is enabled using ''

后端 未结 30 1303
生来不讨喜
生来不讨喜 2020-11-22 05:08

I am getting the following error when I post back a page from the client-side. I have JavaScript code that modifies an asp:ListBox on the client side.

How do we fix

相关标签:
30条回答
  • 2020-11-22 05:22

    (1) EnableEventValidation="false"...................It does not work for me.

    (2) ClientScript.RegisterForEventValidation....It does not work for me.

    Solution 1:

    Change Button/ImageButton to LinkButton in GridView. It works. (But I like ImageButton)

    Research: Button/ImageButton and LinkButton use different methods to postback

    Original article:

    http://geekswithblogs.net/mahesh/archive/2006/06/27/83264.aspx

    Solution 2:

    In OnInit() , enter the code something like this to set unique ID for Button/ImageButton :

    protected override void OnInit(EventArgs e) {
      foreach (GridViewRow grdRw in gvEvent.Rows) {
    
      Button deleteButton = (Button)grdRw.Cells[2].Controls[1];
    
      deleteButton.ID = "btnDelete_" + grdRw.RowIndex.ToString();           
      }
    }
    

    Original Article:

    http://www.c-sharpcorner.com/Forums/Thread/35301/

    0 讨论(0)
  • 2020-11-22 05:22

    I implemented a nested grid view and i faced the same problem .I have used LinkButton instead of image button like this:

    before i had a column like this:

    <asp:TemplateField ItemStyle-Width="9">
      <ItemTemplate>
     <asp:ImageButton ID="ImgBtn" ImageUrl="Include/images/gridplus.gif" CommandName="Expand"
                            runat="server" />
      </ItemTemplate>
    </asp:TemplateField>
    

    I have replaced like this.

    <asp:TemplateField>
    <ItemTemplate>
         <asp:LinkButton  CommandName="Expand" ID="lnkBtn"  runat="server" ><asp:Image  ID="Img"  runat="server" ImageUrl="~/Images/app/plus.gif" /></asp:LinkButton>
          </ItemTemplate>
    </asp:TemplateField> 
    
    0 讨论(0)
  • 2020-11-22 05:24

    I had the same problem with a Repeater because I had a web-page with a Repeater control in a web-site which had EnableEventValidation switched on. It wasn't good. I was getting invalid postback related exceptions.

    What worked for me was to set EnableViewState="false" for the Repeater. The advantages are that it is simpler to use, as simple as switching event validation off for the web-site or web-page, but the scope is a lot less than switching event validation off for either.

    0 讨论(0)
  • 2020-11-22 05:24

    None of the above worked for me. After more digging I realized I had overlooked 2 forms applied on the page which was causing the issue.

    <body>
    <form id="form1" runat="server">
    <div>
            <form action="#" method="post" class="form" role="form">
            <div>
            ...
            <asp:Button ID="submitButton" runat="server"
            </div>
    </div>
    </body>
    

    Be aware that recently ASP.NET has started considering iframes within a form tag which contains a form tag in the iframe document itself a nested frame. I had to move the iframe out of the form tag to avoid this error.

    0 讨论(0)
  • 2020-11-22 05:24

    3: I changed my button type in grid column from "PushButton" to "LinkButton". It worked! ("ButtonType="LinkButton") I think if you can change your button to other controls like "LinkButton" in other cases, it would work properly.

    I wish I could vote you up, Amir (alas my rep is too low.) I was just having this problem and changing this worked like a champ on my gridview. Just a little aside, I think the valid code is: ButtonType="Link"

    I suspect this is because when you click 'edit', your edit changes to 'update' and 'cancel' which then change back to 'edit' on submit. And these shifting controls make .net uneasy.

    0 讨论(0)
  • 2020-11-22 05:25

    The problem is that ASP.NET does not get to know about this extra or removed listitem. You got an number of options (listed below):

    • Disable eventvalidation (bad idea, because you lose a little of security that come with very little cost).
    • Use ASP.NET Ajax UpdatePanel. (Put the listbox in the Updatepanel and trigger a update, if you add or remove listbox. This way viewstate and related fields get updates and eventvalidation will pass.)
    • Forget client-side and use the classic postback and add or remove the listitems server-side.

    I hope this helps.

    0 讨论(0)
提交回复
热议问题