Moksha
Moksha

Reputation: 1030

ASP.NET Calling Jquery Ajax Method from WebUserControl

I am using asp.net3.5 C# with Jquery

I have created a webUserControl timer.ascx, I have a page showTime.aspx I have added timer.ascx into ShowTime.aspx on drodown change I want to display the current time.

but I am getting this error

This type of page is not served. Description: The type of page you have requested is not served because it has been explicitly forbidden. Please review the URL below and make sure that it is spelled correctly.

timer.ascx

[WebMethod]
public static string returnTime()
{
    return DateTime.Now.ToString();
}

showTime.aspx

$(function () {
        $(".currDropDown").change(function () {
            $.ajax({
                type: "POST",
                url: "time.ascx/returnTime",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    $(".timeLbl").text(msg.d);
                }
            });
            return false;
        });
    });

is it that I cant call ajax request inside webusercontrol or there is something wrong in code. please help.

Regards

Upvotes: 1

Views: 1434

Answers (1)

Dave Ward
Dave Ward

Reputation: 60590

You can't host ASP.NET AJAX page methods within ASCX controls, only in ASPX code behind (or, you could use an ASMX ScriptService to centralize the functionality).

Upvotes: 4

Related Questions