views:

37

answers:

2

I have a webcontrol with two dropdownlists on in. When you choose something from the first, the second is populated accordingly. When post back occurs I get the old:

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 know what this problem is; I am populating the dropdown with unexpected data. I've read up on the error and found fixes like overriding the Render method where you call "RegisterForEventValidation" for each new dropdownlist item. I can't do this, however, as I am repopulating the dropdown client side.

In theory I could use this method if I added every possible value (around 3000) that could be included in the dropdown, but I didn't really want to do this.

Are there any other ways of getting around this error, without disabling EventValidation or populating the dropdownlist server side?

A: 

You might have to use a basic html select and look for the selected value on the Request.Form collection.

With the DropdownList I'm afraid you have no other option but those 2.

Claudio Redi
Thanks a lot, I used the Select and it was exactly what I needed. Cheers.
Gareth Voyzey
A: 

Unfortunately, there is no support for disabling event validation on a per-control basis. In my experience, though, disabling event validation on a page is not all that bad.

Regardless of whether event validation is enabled, wise developers validate all input. Tools like the AntiXSS library can come in handy for identifying and/or sanitizing potentially malicious input.

If you are already following practices where you carefully validate form input, then disabling the out-of-the-box ASP.NET validation behavior should be a viable solution that doesn't add substantial risk to your project.

kbrimington
Thanks for your reply. I will look into that library. The reason I can't disable event validation is that this webcontrol can be used from the majority of the sites pages and I wouldn't like to disable validation for the whole site. Would be ideal if I could disable it just on the webcontrol itself, but I'm pretty sure I can't
Gareth Voyzey
@Gareth - Good luck. I have also been disappointed that validation cannot be disabled on a per-control basis. You may find @Claudio's recommendation to use a generic HTML select to be sufficient for your needs.
kbrimington