Invalid postback or callback argument. Event validation is enabled using '<pages enableEventValidation="true"/>'
Do you have codes in you Page_Load events? if yes, then perhaps by adding the following will help.
if (!Page.IsPostBack)
{ //do something }
This error is thrown when you click on your command and the Page_load is being ran again, in a normal life cycle will be Page_Load -> Click on Command -> Page_Load (again) -> Process ItemCommand Event
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.
I had an experience with DataGrid. One of it's columns was "Select" button. When I was clicking "Select" button of any row I had received this error message:
"Invalid postback or callback argument. Event validation is enabled using 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 changed several codes, and finally I succeeded. My experience route:
1) I changed page attribute to EnableEventValidation="false"
. But it didn't work. (not only is this dangerous
for security reason, my event handler wasn't called: void Grid_SelectedIndexChanged(object sender, EventArgs e)
2) I implemented ClientScript.RegisterForEventValidation
in Render method. But it didn't work.
protected override void Render(HtmlTextWriter writer)
{
foreach (DataGridItem item in this.Grid.Items)
{
Page.ClientScript.RegisterForEventValidation(item.UniqueID);
foreach (TableCell cell in (item as TableRow).Cells)
{
Page.ClientScript.RegisterForEventValidation(cell.UniqueID);
foreach (System.Web.UI.Control control in cell.Controls)
{
if (control is Button)
Page.ClientScript.RegisterForEventValidation(control.UniqueID);
}
}
}
}
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.