views:

118

answers:

2

Hi,

I have a web page with various controls. Two of them are dropdownlists. The first dropdownlist gets populated from an xml file on the page_load event. This works fine. To the first dropdownlist a cascadingdropdownlist extender is attached which calls a webservice each time the selection in the first dropdownlist is changed. This works fine too. Underneath my two dropdownlists I have a button which posts the page back. However, when I have made a selection in the second dropdownlist and click the button I get the following error:

Server Error in '/' Application. 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. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: 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.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[ArgumentException: 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.] System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +10945696 System.Web.UI.WebControls.DropDownList.LoadPostData(String postDataKey, NameValueCollection postCollection) +72 System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +507 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2071

Sorry for the terrible formatting. Any suggestions on why this error is thrown and how to prevent it?

Thanks,
Ben

+2  A: 

It's a known problem with the AJAX CascadingDropDown extender.

In order for it to work properly, you need to disable event validation.

Here's a thread discussing the issue: http://forums.asp.net/t/1032053.aspx

I had the same issue, that's why i ditched using the CascadingDropDown extender and just used regular client-side dropdownlist's and some jQuery.

You've got two options:

  1. Ditch the AJAX CascadingDropDown, replace with regular dropdowns, calling web service on client click with jQuery/javascript.
  2. Disable event validation on the page. (not recommended).

Event validation prevents the state of the page being tampered with between requests. Unfortunately, the AJAX CDDL does exactly this, for whatever reason.

It's not something you should disable just to get the CDDL working, as it will affect the entire page and can cause security issues.

My advice, bite the bullet - ditch the CDDL and replace with jQuery.

RPM1984
Hi RPM, thanks for your reply. Check the link in my post above, this works great and you dont have to turn off event validation on the page.
b3n
+1  A: 

I just found the answer to my own questions.

The problem is that AJAX adds the new values to the dropdownlist but as they're not in the viewstate ASP.NET stops with an error. There is a great blog post here which explains how to solve this problem, it worked great for me.

Basically you just subclass the dropdownlist class which get's rid of the SupportsEventValidation attribute -> ASP.NET does not validate the values anymore and everything runs fine!

Read the whole post here: Subclassing the DropDownList to remove the SupportsEventValidation attribute

b3n
A nifty workaround, but still hacky IMO. don't try and 'hack' something that is already hacky (ie the CDDL). still, your call. =)
RPM1984
I had a look at doing it with update panels but got stuck. I would like to change the text of the second dropdownlist to "Loading..." while the background logic is fetching the values. I'm doing it with an onchange javascript attached to the first dropdownlist. However, the text changes fine but the postback seems to get interrupted and no values are loaded. Any idea how to get around this?
b3n
i am doing the exact same thing. i have one dropdown: select state. when they choose a state, a second dropdown gets the text set to "Loading Cities...", make an ajax call to a web service, binds it to the dropdown. all using jquery and a web service call. (onchange event of 1st ddl). to do it in the background, you'll need to call the web service asynchronously, providing a callback function - then in the callback function, clear the loading text and bind the values..
RPM1984
Could you maybe post some code? That would be a great help.
b3n
nps - ill post some code when i get in the office tomorrow
RPM1984