1

text.ascx CODE:

<script type = "text/javascript">
    function ShowCurrentTime() {

        $.ajax({
            type: "POST",
            url: "TestAjax.aspx/GetCurrentTime",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
    }
    function OnSuccess(response) {
        alert(response.d);
    }

<input type="checkbox" id='chkl' onclick=ShowCurrentTime();>';

text.ascx.cs code :

[System.Web.Services.WebMethod]
public static string GetCurrentTime(string name)
{
    return "Hello " + name + Environment.NewLine + "The Current Time is: "
        + DateTime.Now.ToString();
}

how to call jquery ajax in usercontrol.

1

3 Answers 3

2

You cannot call WebMethod in UserControl. Try to use WebMethod in WebPage instead.

Have a look Already answered here

Sign up to request clarification or add additional context in comments.

Comments

0

Can you try like this:

$.ajax({
        type: "POST",
        url: "*name.ascx/method",
        data: { }   ,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            var URL = msg.d;
            alert(URL);
             }
         )};

Hope this helps..

2 Comments

ShowCurrentTime(), its function i have to call usercontrol page
I don't think you can call a page method declared within an .ascx user control. It has to be in a .aspx page or in .asmx web service.
0

you can try this code.

$.ajax({
    type: "POST",
    url: "/yourURL",
    dataType: "json",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        edata = $(data).find("string").text();
        alert(edata);
    },
    error: function(e){
               alert('err');
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.