0

I have the function below which i use to add items to a listbox and it can add the items and i save my changes on button click.

function add_Items() {
        var gdOption = document.createElement("OPTION");

        for (var i = 0; i < document.getElementById("ListBox_AllItems").length; i++) {
            if (document.getElementById("ListBox_AllItems").options[i].selected == true) {

                gdOption.text = document.getElementById("ListBox_AllItems").options[i].text;
                gdOption.value = document.getElementById("ListBox_AllItems").options[i].value;
                document.getElementById("ListBox_ItemsRecovered").add(gdOption);
                document.getElementById("ListBox_AllItems").removeChild(document.getElementById("ListBox_AllItems").childNodes[i]);
            }
        }
        get_all_Items();
    }

The above code has a bug. The bug is if an item has already been added, it can still be selected and added a multiple times which creates duplicates. To try and fix that bug i modified the function as below.

function add_Items() {
        var sourceSelection;
        var targetSelection;
        var i;

            sourceSelection = document.getElementById('ListBox_AllItems');
            targetSelection = document.getElementById('ListBox_ItemsRecovered');
            i = sourceSelection.options.length;
            while (i--) {
                if (sourceSelection.options[i].selected) {
                    targetSelection.appendChild(sourceSelection.options[i]);
                }
            }
        
        get_all_Items();
    }

The above modification has created a bigger problem now. After adding items, now when i click the save button to save my changes, the page posts back as it was doing before but throws the below error, does any one know how i can fix this problem. Now the error it is throwing is below.

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.

I have read posts mentioning that using if(!page.ispostback) {code} in page load before databinding my listboxes would solve the issue but that has not worked. I'm targeting .NET 4.7.2

Alternatively, how would i modify my original function that was not throwing this exception such that it does not add duplicates to my listbox?

StackTrace
  • 9,190
  • 36
  • 114
  • 202
  • Ideally first function should also give similar issue. As I remember when you try to alter element inside dropdown without knowing server (without postback). server assume that some one is trying to alter something which is not correct. It validates data what was sent to the browser and what is sent by the browser as a post back. – शेखर Apr 07 '22 at 15:23
  • One solution could be put the value to `EnableEventValidation="false"` which is not recommended. In this case it will not validate the post back values of the controls. – शेखर Apr 07 '22 at 15:26
  • Similar question https://stackoverflow.com/questions/4572632/modifying-control-with-javascript-invalid-postback-or-callback-argument https://stackoverflow.com/questions/15085836/how-to-pass-event-validation-when-editing-a-select-list-from-javascript https://stackoverflow.com/questions/228969/invalid-postback-or-callback-argument-event-validation-is-enabled-using-page – शेखर Apr 07 '22 at 15:29

0 Answers0