Invalid postback or callback argument. When clicking button
Asked Answered
C

3

5

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

Carolyn answered 3/8, 2010 at 3:10 Comment(0)
C
3

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

Carolyn answered 3/8, 2010 at 3:46 Comment(3)
A nifty workaround, but still hacky IMO. don't try and 'hack' something that is already hacky (ie the CDDL). still, your call. =)Susa
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?Carolyn
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..Susa
S
4

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.

Susa answered 3/8, 2010 at 3:44 Comment(2)
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.Carolyn
damn it! I have a textbox with value "something <root></root>" and the page has an AjaxControlToolKit cascading dropdown, and for some reason the page does not post back to a button click event. SIGH, good answer, thanks.Aristate
C
3

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

Carolyn answered 3/8, 2010 at 3:46 Comment(3)
A nifty workaround, but still hacky IMO. don't try and 'hack' something that is already hacky (ie the CDDL). still, your call. =)Susa
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?Carolyn
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..Susa
S
0

I've searched a lot and ended up with this workaround: http://avinashsing.sunkur.com/2011/03/24/dropdownlist-in-asp-net-does-not-retain-control-state/

for Generic User, in your Parent Page:

Protected Sub cbGenericDropDownList_DataBound(sender As Object, e As System.EventArgs) If Not IsNothing(HttpContext.Current.Request(DirectCast(sender, DropDownList).UniqueID)) Then DirectCast(sender, DropDownList).SelectedValue = _ HttpContext.Current.Request(DirectCast(sender, DropDownList).UniqueID) End If End Sub

and then for each DropDownList Control in your page add: OnDataBound="cbGenericDropDownList_DataBound"

Surf answered 3/10, 2012 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.