-1

i have a script function that can call a web methods with two parameters.I want to call this script function from code behind.Here is my script function that i want to call from code behind.(I am using the .ascx usercontrol).

function BindApporment(obj, divid) {
        debugger;
        var username = "<%= Session["UserId"]%>";
        var id = document.getElementById('<%=Hiddendocid.ClientID%>').value;
        if (id != "") {
            username = id;
        }
        $.ajax({
            type: "POST",
            url: "Dashboard.aspx/BindAppointment",
            contentType: "application/json;charset=utf-8",
            data: "{'date':'" + obj + "',userid:'" + username + "'}",
            dataType: "json",
            success: function (data) {
                $(divid).html(data.d);
            },
            error: function (result) {
                alert("Error ");
            }
        });
    }

here is my web method

[WebMethod]
        public static string BindAppointment(DateTime date, string userid)
        {
        }

...I want to call the script function BindApporment() from code behind..i have already tried a lot of code but this is not working.. I have tried these code but not working:

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "BindApporment", "<script>BindApporment(" + date.AddDays(k).Date + "," + ddldoc.SelectedValue + ");</script>");

any suggestions??

klashar
  • 2,519
  • 2
  • 28
  • 38
Manoj Maharana
  • 52
  • 1
  • 12
  • Are you getting any client side errors? Is your code reached? Where does execution cease to work as you expect? You need to do some basic debugging here. – mason Feb 06 '17 at 13:56
  • The problem is that to call function – Manoj Maharana Feb 06 '17 at 13:57
  • You are trying to call a _client_ method which in turn calls a _server_ method?? Why don't you call server method directly? What special you are doing between these round trips? – Rahul Singh Feb 06 '17 at 14:04
  • It seems like you have a fundamental misunderstanding about how asp.net works. The code-behind and page class in ASP.Net exist for a brief time only to render the html response to an http request. Once that response is rendered, the page class instance is immediately disposed and collected. While the user is views the live page in your browser, the server-side view of the page is already dead; it doesn't exist. Similarly, when you trigger a server event from the page, it's sending a whole new http request from the browser. The DOM from the page is destroyed to make way for a new response. 1/2 – Joel Coehoorn Feb 06 '17 at 14:20
  • In other words, the javascript/DOM view and the c# asp.net code-behind view of a page never even exist at the same time. When you're looking at one, all you can do is prepare signals for the other to receive and process when it's finally created. 2/2 – Joel Coehoorn Feb 06 '17 at 14:21

1 Answers1

0

You are trying to do that is not applicable to to in ASP.NET structure. A ascx control cannot process request. The ASCX can work with the data send via postbacl

You need to create ASMX service and query it with ajax request you have. How to let an ASMX file output JSON

Community
  • 1
  • 1
klashar
  • 2,519
  • 2
  • 28
  • 38