0

Our asp.net webform has two DropDownLists (DdlStatus and DdlPlanStatus) and a "Search" button with server-side code.

Initially, the page would work this way: If ListItem "Plan" was selected in DdlStatus (that's always visible), then DdlPlanStatus would be displayed (using javascript) and the user would select from this 2nd DropDownList and do a search. If ListItem "Complete" was selected in DdlStatus, DdlPlanStatus would be hidden and a search was made. This works fine.

The new change was that the 2nd dropdown (DdlPlanStatus) would be visible with two ListItems. So essentially, If ListItem "Complete" was selected in DdlStatus DdlPlanStatus would have two ListItems to select from.

So this is essentially my javascript code:

function HideDropDown() {
    var ddl = document.getElementById("<%=DdlStatus.ClientID%>");
    var SelectedValue = ddl.options[ddl.selectedIndex].value;
    var ddlParms = document.getElementById("<%=DdlPlanStatus.ClientID%>");
    if ((SelectedValue) == "Complete") {
        // ddlParms.style.display = "none";  //This was before, where I hid dropdown
        document.getElementById("<%=DdlPlanStatus.ClientID%>").options.length = 0;

        var opt = document.createElement("option");

        opt.text = "Complete";
        opt.value = "Complete";
        document.getElementById("<%=DdlPlanStatus.ClientID%>").options.add(opt);


        var opt2 = document.createElement("option");
        opt2.text = "Missing";
        opt2.value = "Missing";
        document.getElementById("<%=DdlPlanStatus.ClientID%>").options.add(opt2);

    }
    else {
        ddlParms.style.display = "block"; //display dropdown
    }
}

When "Complete" is selected in 1st dropdown, I clear the contents of the 2nd dropdown and add 2 items, instead of just hiding it.

The issue is that when I run the search, I get error. I'm almost sure it's because I'm adding the items using javascript:

Invalid postback or callback argument.  Event validation is enabled using
<pages enableEventValidation="true"/> in configuration or 
<%@ Page EnableEventValidation="true" %> in a page.

Do I need to add those ListItems another way?

Thanks.

fdkgfosfskjdlsjdlkfsf
  • 3,165
  • 2
  • 43
  • 110
  • That's a very strange way of clearing the option list. Have you considered [other ways](http://stackoverflow.com/questions/3364493/how-do-i-clear-all-options-in-a-dropdown-box)? – Andrei Oct 07 '15 at 13:45
  • Can you show us the aspx code and c# code for the search button? – psoshmo Oct 07 '15 at 13:46
  • @Andrei, I cleared the list using this other option, but I continue getting the error. So the issue is not the way I clear the dropdownlist. – fdkgfosfskjdlsjdlkfsf Oct 07 '15 at 13:50
  • @psoshmo: to be more specific, I get the error when I click the search button after I select "Complete" in the 1st dropdown and the server-side code reads the value from the 2nd dropdown. – fdkgfosfskjdlsjdlkfsf Oct 07 '15 at 13:53
  • Right, that's why I want to see the relevant code for the search button – psoshmo Oct 07 '15 at 13:54
  • 1
    When you enable eventvalidation, your dropdowns will register their events for all the values they have when being rendered, i.e. when your page get's created on server side. On postback/callback, the passed arguments (values of your dropdowns) get validated against the registered events. So, when you change the dropdowns values on client side (javascript) you have to either turn off eventValidation or change your dropdown list elements on server side. – Markai Oct 07 '15 at 13:56
  • @Markai, I think you're right. The items in the dropdownlist that are initially displayed when the page is rendered are added at design time. – fdkgfosfskjdlsjdlkfsf Oct 07 '15 at 14:00
  • 1
    You can read more about this behaviour here: https://msdn.microsoft.com/en-us/library/system.web.ui.page.enableeventvalidation(v=vs.110).aspx – Markai Oct 07 '15 at 14:00
  • @Markai, very cool piece of info, thanks for sharing. Had no idea list controls register their items for validation purposes – Andrei Oct 07 '15 at 14:08
  • I disabled eventvalidation, but the issue I'm having is that it doesn't recognize the new values in the dropdownlist. It just recognizes the old values. – fdkgfosfskjdlsjdlkfsf Oct 07 '15 at 19:28
  • For example, the first value of `DdlPlanStatus` (2nd dropdown) by default is "Plan". I change the first dropdownlist and now the 2nd dropdownlist has different values. When I click on search, the codebehind registers the value as "Plan" instead of "Complete" (the new value). – fdkgfosfskjdlsjdlkfsf Oct 07 '15 at 19:29
  • @Markai, if you want to, you can post your answer and I'll select it. – fdkgfosfskjdlsjdlkfsf Oct 07 '15 at 19:50

2 Answers2

0

the error is caused by you alter the items of DdlPlanStatus at client side. If DdlPlanStatus always having two options ("Complete" and "Missing"), then you can do it in your aspx file instead of adding them at client side. For more info about EventValidation, please refer to

http://blogs.msdn.com/b/amitsh/archive/2007/07/31/why-i-get-invalid-postback-or-callback-argument-errors.aspx

terryfkjc
  • 191
  • 1
  • 4
0

When you enable eventvalidation, your dropdowns will register their events for all the values they have when being rendered, i.e. when your page get's created on server side. On postback/callback, the passed arguments (values of your dropdowns) get validated against the registered events. So, when you change the dropdowns values on client side (javascript), the registered values don't match the actual values passed on postback/callback and an exception is being thrown. To avoid this, you have to either turn off eventValidation or use the RegisterForEventValidation function (more on this here). Or you change your dropdown list elements on server side if possible.

You can read more about the event validation here

EDIT: If the dropdown values are sensitive data and can cause security issues, it is not recommendable to turn off event validation. Instead it would be better to register all values that are acceptable.

Community
  • 1
  • 1
Markai
  • 2,098
  • 1
  • 10
  • 15