Do you have code in your Page_Load events? if yes, then perhaps 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 it would 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):
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.
You are really going to want to do 2 or 3, don't disable event validation.
There are two main problems with adding items to an asp:listbox client side.
The first is that it interferes with event validation. What came back to the server is not what it sent down.
The second is that even if you disable event validation, when your page gets posted back the items in the listbox will be rebuilt from the viewstate, so any changes you made on the client are lost. The reason for this is that a asp.net does not expect the contents of a listbox to be modified on the client, it only expects a selection to be made, so it discards any changes you might have made.
The best option is most likely to use an update panel as has been recommended. Another option, if you really need to do this client side, is to use a plain old <select>
instead of an <asp:ListBox>
, and to keep your list of items in a hidden field. When the page renders on the client you can populate it from a split of your text field contents.
Then, when you are ready to post it, you repopulate the hidden field's contents from your modified <select>
. Then, of course, you have to split that again on the server and do something with your items, since your select is empty now that it's back on the server.
All in all it's a pretty cumbersome solution that I would not really recommend, but if you really have to do client-side modifications of a listBox, it does work. I would really recommend you look into an updatePanel before going this route, however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With