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?