I am using SharePoint Visual webpart where in I have to use JavaScript or JQuery to call c# method on drop down selected index change to avoid postback.
I have used following code
<asp:DropDownList ID="drpClient" runat="server" DataTextField="ClientName" DataValueField="ClientID" onchange="GetCountryDetails()"></asp:DropDownList>
Below is the JavaScript function.
<script type="text/javascript">
function GetCountryDetails() { debugger; var parm = document.getElementById('<%= drpClient.ClientID %>'); var strCountry = parm.options[parm.selectedIndex].text; var SelectedText = strCountry; var SelectedVal = parm.options[parm.selectedIndex].value;
alert(SelectedVal);
$.ajax({ type: "POST", url: 'Client.aspx/GetPreviousMonthServiceData1', data: "", contentType: "application/json; charset=utf-8", dataType: "json", success: function (result) { if (CallBackFunction != null && typeof CallBackFunction != 'undefined') { CallBackFunction(result); }
}, error: function (result) { alert('error occured'); alert(result.responseText); //window.location.href = "FrmError.aspx?Exception=" + result.responseText; }, async: true }); }
</script>
C# code in ascx.cs is:
publicstaticvoidGetPreviousMonthServiceData1()
{
}
It is not working... It always throws exception saying - Unknown Web method...
</div> </div> </div>