locked
Calling C# method from JavaScript in .ascx file RRS feed

  • Question

  • Hi,

    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:

    publicstaticvoidGetPreviousMonthServiceExcellenceData1()

            {

            }

    It is not working... It always throws exception saying - Unknown Web method...


    Regards, Shreyas R S

    Monday, October 12, 2015 5:20 AM

All replies

  • Check the names: “GetPreviousMonthServiceData1” vs “GetPreviousMonthServiceExcellenceData1”.

    Monday, October 12, 2015 6:15 AM
  • I have modified that too!!

    Sorry forgot to update here...


    Regards, Shreyas R S

    Monday, October 12, 2015 6:25 AM
  • Try accessing “Client.aspx/GetPreviousMonthServiceData1” from the browser directly, were you will have to put the full URL that includes the SharePoint base URL. Are you sure that SharePoint accepts such format of addresses? Also try removing 'static'.

    If it works, or gives a different error that can be ignored for now, then try specifying the full URL in your JavaScript too.


    • Edited by Viorel_MVP Monday, October 12, 2015 6:52 AM
    Monday, October 12, 2015 6:51 AM
  • I tried accesssing url in browser, it workst still function is faling and throws error!!

    Regards, Shreyas R S

    Monday, October 12, 2015 2:37 PM
  • Hi sosh2785,

    Thank you for your post. Your question is more related to ASP.NET, I suggest you create a new thread on ASP.NET forum. You will get more help there.
    http://forums.asp.net/1022.aspx/1?ASP+NET+AJAX+Ajax+Control+Toolkit

    Best Regards,
    Li Wang

    Tuesday, October 13, 2015 5:19 AM
    Moderator