Invalid postback or callback argument. Event validation is enabled using in configuration

I wrote web part that uses Repeater control to create some repeating blocks of output. Each of these blocks has Button control in it. When I ran web part under SharePoint I got the following error: 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. Here is the solution.

Button control in Repeater item template has ID assigned to it. If Repeater data source has more than one row then this exception is thrown. My solution was simple. As I had ItemDataBound event handler written anyway I solved the problem there. All I had to do was to change Button control ID so it is unique. Here is my repeater (actually, very-very simplified version of it).

<asp:Repeater runat="server" ID="companiesRepeater"> 
<ItemTemplate>
 
   
<div class="info-row">
 
       
<div class="title">
 
           
<h5>Firma</h5>
 
       
</div>
 
       
<div class="information-main">
 
           
<asp:Label runat="server" ID="companyName"></asp:Label>
 
       
</div>
 
   
</div>
 
   
<div class="submit" runat="server" id="buttonContainer">
 
       
<asp:Button runat="server" ID="editCompanyButton" Text="Edit"></asp:Button>
 
   
</div> 
</ItemTemplate>               
</asp:Repeater
>

To bind repeater with data I am using code like this.

public void BindCompanies()
{
   
var list = SPContext.Current.Site.OpenWeb("/companies/").Lists["Pages"
];

   
var queryString = "<Where><Eq><FieldRef Name='Author' />"
;
    queryString +=
"<Value Type='User'>{0}</Value></Eq></Where>"
;
    queryString =
string
.Format(queryString, Item.Title);

   
var query = new SPQuery
();
    query.Query = queryString;

   
var
items = list.GetItems(query);
   
if (items == null
)
       
return;

    companiesRepeater.DataSource = items;
    companiesRepeater.DataBind();
}

When I try to bind repeater to data then everything is okay. Error occurs when I try to push editCompanyButton because buttons in ItemTemplate have similar ID-s. My solution here simple one and it works. At least for me.

protected void companiesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   
if (e.Item.DataItem == null
)
       
return
;

   
var
item = (SPListItem)e.Item.DataItem;

   
// uninteresting code

   
var button = (Button)e.Item.FindControl("editCompanyButton"
);
    button.CommandArgument = item.ID.ToString();
    button.ID =
"button_" + e.Item.ItemIndex;
    button.Click += editCompanyButton_Click;
}

As you can see the solution is simple: each button in item template has now different ID and error mentioned above doesn’t occur anymore.

Liked this post? Empower your friends by sharing it!

Gunnar Peipman

Gunnar Peipman is ASP.NET, Azure and SharePoint fan, Estonian Microsoft user group leader, blogger, conference speaker, teacher, and tech maniac. Since 2008 he is Microsoft MVP specialized on ASP.NET.

    22 thoughts on “Invalid postback or callback argument. Event validation is enabled using in configuration

    • May 11, 2009 at 6:11 am
      Permalink

      No, it is not turkish. It is estonian language. Firma means company. :)

    • May 26, 2009 at 10:40 am
      Permalink

      Just add this to your web config and make pages..like ErrorPage.aspx , and see you will control this error by making your custom control error.

    • May 26, 2009 at 10:58 am
      Permalink

      Thanks for your feedback, Vivek. Using error pages doesn’t solve the problem – problem still appears but instead of handling it we will redirect user away and tell him that system has problem.

      My solution here solves the problem so I don’t have to interrupt activities that users are doing. Situation is handled nicely and nothing is broken. :)

    • June 23, 2009 at 12:11 pm
      Permalink

      Thanks,

      I was about to start pulling my hair out because of this error. Just had to uniquely name and ID all my controls. Seems like when there are too many similarly named controls on the page that error pops up.

    • June 29, 2009 at 12:46 am
      Permalink

      the issue may raise if you execute the DataBind() Method for the repeater on the PageLoad event because it will re-assign new control IDs before reaching the ItemCommand event for the repeater control. you should call DataBind() with a conditional if (!IsPostBack), however I’m not quite sure about this issue in SharePoint

    • July 31, 2009 at 6:18 pm
      Permalink

      If we are talking about the ID attributes of your controls in the page HTML, then remember that per the HTML specs, ID attributes should always be unique anyway.. it’s invalid HTML to have the same ID appear more than once on a page. Name can be repeated, and Class but not ID.

      Also be aware that when/if you try to create browser level automation to test the site, it’s a lot easier for whoever has to do that to properly idenfify instances of controls if they have some unique proper (such as id) instead of repeating all the same attribute values for the elements on the page.

    • September 2, 2009 at 4:58 pm
      Permalink

      This was extremely helpful. Thank you for posting this

    • September 28, 2009 at 9:37 am
      Permalink

      I was trying to change the add cusom webpart of the shared page of the sharepoint site. After adding a webpart i got a screen where i selected all the web-parts and after that the startpage of sharepoint site did not contain any webparts anymore. When adding new webparts to the startpage of sharepoint site the following error occurred.

      Server Error in ‘/’ Application.
      ———————————————-

      Invalid postback or callback argument. Event validation is enabled using in configuration or 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.

    • June 8, 2010 at 1:15 pm
      Permalink

      enableEventValidation=”false”

    • June 9, 2010 at 8:52 pm
      Permalink

      Ramavtar, loosening security is choosing easiest but most dangerous way. I don’t suggest it.

    • August 12, 2010 at 11:14 am
      Permalink

      problem seems to persist in an update panel

    • November 23, 2010 at 5:16 am
      Permalink

      I oooooooooooooooo

    • May 28, 2011 at 11:36 pm
      Permalink

      Amr Ellafi you are right if you bind data in pageload event than only this event occur if you write this in IF(!ispostback) block it will not be occur

    • October 9, 2011 at 12:59 pm
      Permalink

      I had a similar problem with DataList.

      Based on your solution I figure this out for VB:

      Protected Sub DataListGalleries_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles DataListGalleries.ItemDataBound
      If e.Item.DataItem Is Nothing Then Return
      Dim item = e.Item.DataItem
      Dim ib = CType(e.Item.FindControl(“ImageButton”), ImageButton)
      ib.CommandArgument = item.ID.ToString()
      ib.ID = “ImageButton_” & e.Item.ItemIndex

      Thanks a lot!

    • November 1, 2011 at 7:12 am
      Permalink

      I have been fighting this issue all day and I stumbled upon this blog. Yup, this one-line fix is the solution!

      button.ID = “button_” + e.Item.ItemIndex;

      Thanks!

    • November 1, 2011 at 7:12 am
      Permalink

      I have been fighting this issue all day and I stumbled upon this blog. Yup, this one-line fix is the solution!

      button.ID = “button_” + e.Item.ItemIndex;

      Thanks!

    • December 2, 2011 at 4:24 am
      Permalink

      Just came across getting this issue. You’re solution helped me resolve it quickly. Great help!

    • May 31, 2012 at 10:25 pm
      Permalink

      It is so easy solution that I hardly belived it can work. Changing button’s ID realy saved my life.

      Thank You :)

    • October 13, 2012 at 12:24 pm
      Permalink

      I hardly comment, but i did some searching and wound up
      here Invalid postback or callback argument. Event validation is enabled using in
      configuration … – Gunnar Peipman’s ASP.NET blog. And I do have a few questions for you if you do not mind. Could it be just me or does it look like a few of these comments appear like written by brain dead visitors? :-P And, if you are writing on additional online social sites, I would like to keep up with anything fresh you have to post. Would you list of every one of all your public sites like your linkedin profile, Facebook page or twitter feed?

    • October 13, 2012 at 6:09 pm
      Permalink

      No, Joe, it’s spam. Spam that MS is not able to stop and I have hard times fighting with it :)

    • November 18, 2012 at 11:36 am
      Permalink

      I belive the “official” way to solve this is to set UseSubmitBehavior=”false” on the button, then use the CommandName in the button and the OnItemCommand event handler on the repeater wheer you check e.CommandName

      So, no need to mess with changing id values on the controls.

    Leave a Reply

    Your email address will not be published. Required fields are marked *