I am trying to trigger a postback handler when the user selects an option from a select2 dropdown. When I try using the code shown below, I receive the error message:
"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 am looking for advice on a method to use the select2 event to trigger the postback and thus trigger loading new data based on the user selection
ASCX control
<%@ Control Language="VB" AutoEventWireup="false" Inherits="uc_selector" EnableViewState="true" Codebehind="selector.ascx.vb" %>
<asp:TextBox ID="txtName" Visible="False" Runat="server" AutoPostBack="False" />
<asp:HiddenField ID="txtID" runat="server" />
<asp:ListBox ID="nameSelect" Runat="server" AutoPostBack="False" />
<script type="text/javascript">
$(document).ready(function() {
$("#<%=nameSelect.ClientID%>").select2({
minimumInputLength: 2,
width: 'resolve',
ajax: {
url: "<%=ResolveClientUrl("~/")%>MyEndpoint.ashx",
dataType: 'json',
data: function (params) {
return {
q: params.term
};
},
processResults: function (data) {
return {
results: $.map(data,
function(x) {
return {
id: x.Id,
text: x.Name
};
})
};
}
}
});
$("#<%=nameSelect.ClientID%>").on('select2:select', function (e) {
$("#<%=txtID.ClientID%>").val(e.params.data.id);
$("#<%=txtName.ClientID%>").val(e.params.data.text);
__doPostBack('<%=nameSelect.UniqueID%>', '');
});
});
</script>
Code Behind
Protected Sub name_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles nameSelect.TextChanged
RaiseEvent TextChanged(sender, e)
End Sub