Reputation: 67233
I am using JavaScript/jQuery to dynamically adding elements to a dropdown list on an WebForms page. This is working fine, but when I post the page back to the server, I get the following error:
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 think I understand what is happening here: ASP.NET is verifying that the list is the same one that was originally served and detects that it has changed. This is a security feature because this indicates data is being submitted from a modified page.
However, in this case, the behavior is valid. And I would prefer not to disable this feature because it is a good security feature for the rest of my page.
Is there any way to tell ASP.NET to ignore only changes to this one list?
EDIT:
My code is in a Web User Control so I thought it might be acceptable to set the EnableEventValidation
property for the control. Unfortunately, it appears this property is only available for the entire page.
Upvotes: 0
Views: 652
Reputation: 67233
I found I was able to eliminate this error by creating a custom DropDownList control like this:
public class DropDownListNoEventValidation : DropDownList
{
}
This apparently creates a control called DropDownListNoEventValidation
without the attribute SupportsEventValidation
.
The result is elimination of the error I was getting before. Unfortunately, it appears ASP.NET does not recognize the selected value if it is an item that was dynamically added to the list. I'm still working on that problem.
UPDATE:
I ended up using a <select>
element without runat="server"
. This was a bit more work, but seemed the most straight forward way to get this to work as expected.
Upvotes: 1