简体   繁体   中英

Invoking ASP.NET codebehind method from JavaScript

Can someone please tell me how I can invoke a ASP.NET codebehind method from client-side JavaScript?

Thanks

这是一篇很好的文章,介绍如何对代码隐藏方法进行Ajax调用: 使用jQuery直接调用ASP.NET AJAX页面方法

I've always used Page Methods. They are pretty good.

You can read the blogpost by Dave for step by step tutorial: http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/

Use an update panel control is the easiest way, but you can post the page to the server and get the result using Sys.Net.WebRequest: http://msdn.microsoft.com/en-us/library/bb310979.aspx .

That's a lot harder because you have to manually update the response in the page, wipe out the old content, parse the result, and inject the new content. The typical way it is done is to use a web service and call Sys.Net.WebServiceProxy.invoke method: http://msdn.microsoft.com/en-us/library/bb383814.aspx . This can call a web service within the page (page methods) or a separate ASMX or WCF web service.

HTH.

Use jQuery AJAX , This is good source

$.ajax({
  type: "POST",
  url: "MessagePopup.asmx/SendMessage",
  data: "{subject:'" + subject + "',message:'" + message + ",messageId:'" + messageId + "',pupilId:'" + pupilId +"'}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting here.
  }
});

If you want just using JavaSctipt without using jQuery fallow this links

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