简体   繁体   中英

Can we call User control code behind method using Jquery ajax?

I'm trying to do something, Can we call User control code behind method using Jquery ajax ? likes:

ASCX CODE:

<script type="text/javascript">
function ShowCurrentTime() {
    $.ajax({
        type: "POST",
        url: "Uploads.aspx/GetCurrentTime",
        data: '{name: "' + $("#<%=txtUserName.ClientID%>")[0].value + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(response.d);
        }
    });
}
function OnSuccess(response) {
    alert(response.d);
}

ASCX.CS

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

But its will create an error please can any one what do i am wrong.

用户控件不具有页面的所有功能,因此不能直接调用。请查看以下链接以获取完整的解决方案: 使用AJAX,JSON,基类和反射在ASCX用户控件内创建Page方法(ScriptMethod)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM