Nayeem Mansoori
Nayeem Mansoori

Reputation: 831

how to use jQuery ajax with asp.net user controls

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.

Upvotes: 1

Views: 3836

Answers (3)

Nikhil Chavan
Nikhil Chavan

Reputation: 1715

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

Have a look Already answered here

Upvotes: 2

Govinda Rajbhar
Govinda Rajbhar

Reputation: 3034

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');
    }
})

Upvotes: 0

Sai Avinash
Sai Avinash

Reputation: 4753

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..

Upvotes: 0

Related Questions