I needed to make a set of drop downs where the possible values in one depend on the selected value in a previous one, something like:
The AJAX Control Toolkit has a great CascadingDropDownExtender to make this possible. But I learned a couple of things in the process.
1. EventValidation. Everything works fine until your page does something that causes a postback. Then the server sees data in the drop downs that it didn’t expect and gives a big ugly error:
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> 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.
The code samples just turn off EnableEventValidation, but this is a big unnecessary security opening, like taking the walls off the barn to let the cow out. You can also use ClientScriptManager.RegisterForEventValidation, but this requires registering all possible values – a bit of overkill.
Then I found a brilliant post by Johan Leino (http://johanleino.wordpress.com/2009/11/17/cascadingdropdown-casues-invalid-postback-or-callback-argument-error/). He dug in and found that the reason the DropDownLists are validated at all is because they are decorated with the SupportsEventValidation attribute. He shows that you can subclass DropDownList, making no coding changes, and your resulting class does not have the SupportsEventValidation attribute. You use your control instead of the standard DropDownLists and the page won’t do event validation on them.
public class DropDownListNoValidation : DropDownList
2. ASMX vs. WCF Service. While the AJAX Control Toolkit samples use an ASMX service for the callbacks, I thought “I’ll be technically savvy and use WCF instead.” After all, everything you read says Microsoft is considering ASMX as legacy and that WCF is just as fast and gives you lots of richness. Well, maybe it normally does, but I found a lot of latency. When I had 5 DropDownLists on my page, I was seeing a 1-2 second delay on the loading of the controls. I watched this in Fiddler and saw that it was working but that there was a latency lag between when the server received the call and when it returned the data. (And this was all on my own box). I switched back to ASMX and voila! It was super speedy.
I wish I understood *why* I saw the delay. There probably are some configuration settings I should be doing. But for now I’m sticking with the ASMX.