0

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.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • How are you posting back? – Yuriy Galanter Oct 14 '13 at 21:42
  • Just by clicking a server button control. – Jonathan Wood Oct 14 '13 at 21:44
  • Please post the code. – mayabelle Oct 14 '13 at 21:45
  • What code? It's a button. Clicking it posts back automatically. It's not my code that causes this error, it's ASP.NET. – Jonathan Wood Oct 14 '13 at 21:46
  • @JonathanWood Something else is going on. I just tried to recreate a basic scenario (created a dropdownlist with a few ListItem elements server-side, added a few more OPTION elements client-side and posted back via button click) and got no errors – Yuriy Galanter Oct 14 '13 at 21:48
  • @YuriyGalanter: Thanks for checking that. Perhaps I don't understand what is happening afterall. Just to confirm, this is on a WebForms page, correct? – Jonathan Wood Oct 14 '13 at 21:50
  • 1
    Usually this happens when you do a custom call to a `__doPostBack('ControlID', 'someArguments')` any chance you're doing something like this somewhere? – Yuriy Galanter Oct 14 '13 at 21:51
  • It doesn't appear so. The markup is `` and I'm not seeing any JavaScript that references this element. – Jonathan Wood Oct 14 '13 at 21:54
  • @YuriyGalanter: In your example, does setting the page property `EnableEventValidation="true"` make any difference? – Jonathan Wood Oct 14 '13 at 21:56
  • Just tried that as well, still no errors. On postback simple original server-side dropdown is rebuild, losing client-side items. I believe `EnableEventValidation="true"` is a default setting when ommited – Yuriy Galanter Oct 14 '13 at 21:57
  • @YuriyGalanter: Yes, I thought that was the default as well. According to the second answer for http://stackoverflow.com/questions/7476329/asp-net-invalid-postback-or-callback-argument, this is caused when modifying a list. In my case, I'm adding a single item to the control and then *selecting the new item*. Could you please try selecting the added item? – Jonathan Wood Oct 14 '13 at 22:01

1 Answers1

1

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.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466