CascadingDropDown causes invalid postback or callback argument error

Update 2011-04-09
I have created a new post that included an example application here.

Have a look at this markup:

In this page I have three dropdown lists with the CascadingDropDown extender attached to them, not going to go into details how I set them up (maybe some other time) though.
The issue was in this situation that when I clicked the “Save” button the error that you can see below appeared.


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. at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
at System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument)
at System.Web.UI.WebControls.DropDownList.LoadPostData(String postDataKey, NameValueCollection postCollection)
at System.Web.UI.WebControls.DropDownList.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection)
at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Apparently it seems that when the items in the dropdowns where altered by client side code (as in the case with the CascadingDropDown extender) the asp.net runtime will recognize that the items in the dropdown are different from what was put in the viewstate which is not allowed.
You could of course set the EnableEventValidation=”false” as the page directive but we don´t really want to do that, do we?

The “badguy” apparently is the SupportsEventValidationAttribute that tells asp.net to perform this validation on the dropdowns which have this attribute

.

As MSDN says:
“If you do not define the SupportsEventValidationAttribute attribute, your custom control does not automatically participate in event validation”.

So the solution was to construct a custom class that inherits from DropDownList but does not include this attribute, like this:

So the new markup looks something like this:

…and now the click event fires like expected and the other validation logic on my page is still intact.

, ,

  1. #1 by sq33G on December 7, 2009 - 14:26

    Excellent. I just spent a day trying to figure this one out. Thanks.

    • #2 by Johan Leino on December 7, 2009 - 22:18

      Great! That is why I posted the article. I had also spent quite some time with it…glad I could help you.

    • #3 by kenn chongwo on November 12, 2010 - 18:43

      Hi, Chief, i realy need uo help on this dropdown thing, how did u achieve it, how were u able to subclass drpdownlist and add to your tool box, please help.

      • #4 by Johan Leino on November 12, 2010 - 20:39

        Do you mean how to subclass a dropdown list? Isn´t that already in the post you think?
        Construct a class and derive it from DropDownList and then add a reference to that class in the page directive of your page…

        If you need some more help can you please be a little more precise on what you need help with?

  2. #5 by Nilesh on December 16, 2009 - 09:07

    Hello Can you please provide sample code, how to do it??

    I am clueless 😦 Thanks 🙂

    • #6 by Johan Leino on December 16, 2009 - 13:36

      What do you mean? How to setup a cascading drop down or something else? Could you be a bit more precise on what you want?

  3. #7 by raf on March 15, 2010 - 14:40

    How do you register the .vb class in the designer page?????
    it is imposible for me.

    :S :S

    • #8 by Johan Leino on March 15, 2010 - 15:43

      You have to reference the assembly, either do that in web.config or in the page directives at the top of your page. Something like:

      In web.config under pages/controls:
      add tagPrefix=”jl” namespace=”JL.TestProject.WebControls” assembly=”Jl.TestProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b08d1987b18b292a”

      Or as page directive:
      @ Register Assembly=”Jl.TestProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b08d1987b18b292a” TagPrefix=”JL”

  4. #9 by raf on March 15, 2010 - 14:42

    I don’t know how to register “jl:NoValidationDropDownList”

    • #10 by Johan Leino on January 31, 2011 - 15:03

      Look at the previous comment I replied to:
      At the top of your page/control
      @ Register Assembly=”Jl.TestProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b08d1987b18b292a” TagPrefix=”JL”

      Replace that with your own assembly name and namespace and optionally your own tagprexix

  5. #11 by Leon on April 27, 2010 - 21:50

    I just spend like 3 days trying to figure out why the pages crashes after being populated with javascript.
    Are you getting all values and selections of your DDLs back on server after postback or do you use hidden fields and so on? is it possible to force whatever engine is recreating the viewstate on client, to include content and properties of controls modified by js? I’m getting 0 items in my DDL after postback.

    • #12 by Johan Leino on April 28, 2010 - 12:20

      I´m using a page method in my aspx.cs file to expose as the values for the DDLs. It looks something like this:
      [WebMethod]
      [ScriptMethod]
      public static CascadingDropDownNameValue[] FindDDLValues(string knownCategoryValues, string category)
      {
      // Get a dictionary of known category/value pairs
      StringDictionary knownCategoryValuesDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
      // Parse as xml
      var Document = FindAllDecisions();

      var results = CascadingDropDown.QuerySimpleCascadingDropDownDocument(Document, Hierarchy, knownCategoryValuesDictionary, category);

      return results;
      }

  6. #13 by sangram on August 27, 2010 - 09:56

    boss i tried to create a custom dropdown control still i get same 503 error any way out

  7. #14 by sangram on August 27, 2010 - 09:58

    here if i set EnableEventValidation=”false” ValidateRequest=”false” the page load occurs but no submit button click fires.

  8. #15 by kenn chongwo on November 10, 2010 - 22:29

    hey chief, thanx alot for this post it shades of light to this troubling cascade thing, could u pliz send me the implementation of your subclass, i mean the subclass extending dropdownlist.. thanx again

    • #16 by Johan Leino on November 10, 2010 - 21:30

      I’ll see if I’ve got that code somewhere. Get back to you…

      • #17 by kenn chongwo on November 12, 2010 - 18:45

        thanx in advance..

  9. #18 by Alexandre Queiroz on January 31, 2011 - 14:44

    Hi there,

    i am following your tutorial but i can´t register the assembly on the top of the page.
    Could you help me ?
    I implemented the .cs class NoValidationDropDownList.cs . But i can´t register it on the page.

    THANK YOU !

    • #19 by Johan Leino on January 31, 2011 - 15:05

      Look at the previous comment I replied to:
      At the top of your page/control
      @ Register Assembly=”Jl.TestProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b08d1987b18b292a” TagPrefix=”JL”

      Replace that with your own assembly name and namespace and optionally your own tagprefix

      • #20 by Alexandre Queiroz on January 31, 2011 - 15:26

        i have implemented the NoValidationDropDownList.cs which has the “public class NoValidationDropDownList : System.Web.UI.WebControls.DropDownList” .
        Now on my MASTERPAGE, im using the cascadingdropwdownlist and im doing this:

        Is it wrong ?

  10. #21 by Alexandre Queiroz on January 31, 2011 - 15:41

    i have implemented the NoValidationDropDownList.cs which has the “public class NoValidationDropDownList : System.Web.UI.WebControls.DropDownList” .
    Now on my MASTERPAGE, im using the cascadingdropwdownlist and im doing this:

    Is it wrong ?

    ps: Please ignore the previous post . I missed a line.

  11. #22 by Alexandre Queiroz on January 31, 2011 - 15:47

    Sorry for the flood. Now this is:
    Register Namespace=”Jl.NoValidationDropDownList” Assembly=”Jl.NoValidationDropDownList, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b08d1987b18b292a” TagPrefix=”JL”

    • #23 by Johan Leino on January 31, 2011 - 18:41

      That is my strong named assembly.
      You have to either figure out what strong name your assembly uses (with reflector or some other tool) or maybe your project isn´t signed and then you don´t need the full 4-part name.
      Anyhow, your @register tag will not look the same as mine (which is just an example).
      Making sense or do you need more explanation?

  12. #24 by anon on March 16, 2011 - 21:44

    worst tutorial ever no useful information just a bunch of notes with no working example project…
    please take down this page you are just confusing everybody which trys to search for good info, thanks to you i got delayed 5 hours in my project…

    • #25 by Johan Leino on March 17, 2011 - 15:02

      OK, I understand your frustration but that doesn´t give you any right to be rude. I you had asked politely I could just have provided you with a working sample…

  13. #26 by Feisal on March 25, 2011 - 22:19

    Hi Johan,
    Really your artical solved a real problem because every one points your article for the solution, for me is ok all other things but registering or referencing the control is not clear to me not only me but most other guys who make comments, so please is it possible to explain littel bit or point us some other place we can get artical talking registerin or referensing, or if some other guys can help us realy is usefull.
    Thanks

  14. #29 by Joy on April 12, 2011 - 04:55

    hii… looked at ur post .. although i have succeeded in implementing this .. but there are stil soem issues … say i want to populate the dropdown using jquery ajax. on a cascading effect . now the problem is on a button click when i am trying to do something like

    textbox1.tex = dropdown1.text its not working . is that to do something with the viewstate ? how to solve it if i use something like jquery ajax ?

    • #30 by Johan Leino on April 12, 2011 - 07:46

      generally I would say that it is bad idea to mix client (JQuery calls via AJAX) code and server controls (aka runat=server) since these depend upon postbacks. Since your button triggers a postback your page gets reloaded and thus the values in your dropdown are lost. With that said I think there may be situations where you might need that and that is where AJax Control Toolkit comes into play. You can call a service with the cascading extender thus making your jquery ajax call obsolete. I haven´t investigated this any further though…

  15. #31 by Joy on April 13, 2011 - 05:09

    yes i am aware of the ajax control toolkit with cascading extender . but its a pretty heck and the viewstate gets heavily loaded. as for that i gues u are aware of ajax control toolkit’s cons. thts d very reason i havent used it . its just a query for the above if i can achieve the above scenario in the earlier post or not .. although i accept i m a newbie . but stil there has to b someway when we can overcome that shortcoming for the client ajax calls via jquery and postback problem ? if you can just see if there are any possible ways … 🙂 thanks in advance 🙂 and this code for the time being saved a lot of life .. a wonderful piece of job i must say 🙂 .. kudos to you 🙂

  16. #32 by Jessica on July 6, 2011 - 16:37

    Hi Johan,
    Could you please give me a working sample?I am stuck with this Invalid Callback for many days and still not able to solve it.pls help.

    • #33 by Johan Leino on July 8, 2011 - 11:55

      Please have a look at the post that I link to here which includes a sample

  17. #34 by abesha on August 29, 2011 - 19:14

    Thanks, it helps me a lot

  18. #35 by kishore sagar on September 2, 2011 - 20:44

    Fantastic work. I did the custom class and everything looks and works good. Thanks ..and keep up the good work. 🙂

  19. #37 by Gajendra Salunkhe on February 8, 2012 - 07:54

    Hi

    Its not working for me. I am doing like this. I am using Framework 4.0 with VS2010

    1) The class file:
    namespace Web.Common
    {

    ///
    /// To have DropDownList without EnableEventValidation
    ///
    public class NVDropDown : System.Web.UI.WebControls.DropDownList
    {
    }
    }

    2) On Page

    — Select —

    Regards
    Gajendra Salunkhe

    • #38 by Johan Leino on February 8, 2012 - 08:15

      Could they (MS) have changed the implementation of this in .NET 4 (asp.net 4) then…??
      Does it work if you switch it back to 3.5 to do a little test??

  20. #39 by Justin on February 12, 2012 - 23:29

    This worked great for me, the only thing i had to do additionally was
    %@ Register Assembly=”Namespace” Namespace=”Namespace” TagPrefix=”Prefix”

    then my tags looked like

    prefix:NoValidationDropDown

    works great! thank you so much

  21. #40 by ejeffre on May 30, 2012 - 19:24

    Parser Error Message: Could not load file or assembly ‘CascadingDropdownExtenderExample’ or one of its dependencies. The system cannot find the file specified.

    I keep getting this, I followed the example above, only differences is I am on VS2008, I created a new class in my project.

    This is totally frustrating at this point.

  22. #42 by Shekhar Dalvi on July 18, 2012 - 06:53

    Great Work… I have Implemented it and works fine however when I set the Text property of the dropdownlist on button’s click it throws an errror “selectedvalue which is invalid because it does not exist in the list of items.parameter name value” here is my code on button’s click

    dd_ins_State.Text = “20”;

    also I have checked Items count in dropdownlist which result into 1 while I can see all the items in dropdownlist. here is my code on button’s click

    int i = dd_ins_State.Items.Count;
    int j = dd_ins_City.Items.Count;

    string state = dd_ins_State.SelectedItem.Text;
    string city = dd_ins_City.SelectedItem.Text;

    dd_ins_State.Text = “20”;

    please help me out.

  23. #43 by Bharath on September 13, 2012 - 08:02

    How to get the values selected in the DDL in the SAVE button click implementation method.

  24. #44 by Jhonatas Kleinkauff on August 7, 2013 - 14:23

    Hey! I just came here to say thanks! This works for me!

  25. #45 by Siddharth on June 16, 2015 - 14:17

    Really kickass article. After 5 hours of googling finally came across this. Your a life saver. Thanks alot and keep up the good work !
    Cheers.

  1. Fun with CascadingDropDownExtenders « The L33t Man
  2. Fun with CascadingDropDownExtenders « Kevin Leetham's Techno Babble
  3. 2010 in review « Johan Leino
  4. Revised: CascadingDropDown casues invalid postback or callback argument error « Johan Leino
  5. exception:Invalid postback or callback argument. Event validation is enable, caused by jQuery

Leave a comment