Today I had an error (one of many if you are a developer) and it threw me off a bit.
I’m using a visual web part (VS 2010 with SP 2010) and it has an asp:Repeater with buttons. Testing the buttons gave me:
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 theClientScriptManager.RegisterForEventValidationmethod in order to register the postback or callback data for validation.
Things That DON’T Work
After a lot of searching, you’ll find the following suggestions — all of which are wrong:
- Add
<pages enableEventValidation="true"/>in the ASCX file — wrong - Add
<%pages enableEventValidation="true"/%>in the ASCX — wrong - Add
public void Page_PreInit(...) { Page.EnableEventValidation = false; }— wrong - Replace all
truewithfalse— wrong
Quick Fix
The only thing you need to do is set EnableViewState="false". 🙂
The More Complex Case
Edit: The above “solution” does fix things, but I discovered later that it won’t help when you want to do special things.
I had an UpdatePanel, inside of it an asp:Repeater, and inside the repeater a FileUpload. That’s a quite different story altogether.
Here’s what worked for me:
- Add triggers to the UpdatePanel:
<Triggers>
<asp:PostBackTrigger ControlID="nameOfYourRepeater" />
</Triggers>
-
Set
EnableViewState="true" -
Because I have multiple controls, the ID cannot be the same. Add an
ItemDataBoundevent to your repeater in the code-behind:
var item = (DataRowView)e.Item.DataItem;
var button = (Button)e.Item.FindControl("ID_NAME");
button.CommandArgument = e.Item.ItemIndex.ToString();
button.ID = "ID_NAME" + e.Item.ItemIndex.ToString();
Every control is unique because the ID is controlName + ItemIndex. There are other ways, but remember — you may also need to retrieve the unique control later, so ItemIndex is the easiest approach.
Hope it helps someone!