I am getting the following error when I post back a page from the client-side. I have JavaScript code that modifies an asp:ListBox on the client side.
How do we fix
I've had the same problem, what I did:
Just added a condition if(!IsPostBack)
and it works fine :)
If you know up front the data that could be populated, you can use the ClientScriptManager to resolve this issue. I had this issue when dynamically populating a drop down box using javascript on a previous user selection.
Here is some example code for overriding the render method (in VB and C#) and declaring a potential value for the dropdownlist ddCar.
In VB:
Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
Dim ClientScript As ClientScriptManager = Page.ClientScript
ClientScript.RegisterForEventValidation("ddCar", "Mercedes")
MyBase.Render(writer)
End Sub
or a slight variation in C# could be:
protected override void Render(HtmlTextWriter writer)
{
Page.ClientScript.RegisterForEventValidation("ddCar", "Mercedes");
base.Render(writer);
}
For newbies: This should go in the code behind file (.vb or .cs) or if used in the aspx file you can wrap in <script>
tags.
I know that this is a super-old post. Assuming that you are calling into your application, here is an idea that has worked for me:
If you don't need total control, you could use an update panel which would do this for you.
If you are using Ajax update panel. Add <Triggers>
tag and inside it trigger the Button or control causing the postBack using <asp:PostBackTrigger .../>
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
I was using datalist and I was getting the same error for my push button. I just use IsPostBack to check and fill my controls and the problem is solved! Great!!!