X

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!
Categories: ASP.NET SharePoint

View Comments (22)

  • 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.

  • 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. :)

  • 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.

  • 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

  • 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.

  • 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.

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

  • 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

  • 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!

  • 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!

  • 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!

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

    Thank You :)

  • 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?

  • 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.

Related Post