jQuery 如何在asp.net webforms中使用ajax

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5331986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:59:29  来源:igfitidea点击:

How to use ajax with asp.net webforms

jqueryasp.net

提问by chobo

Is there any way to use ajax I'm using Jquery for this) with asp.net webforms without having to run through the pages life cycle?

有什么方法可以使用ajax(我为此使用Jquery)和asp.net webforms,而不必运行整个页面生命周期?

回答by Mun

Depending on what you're trying to do, you could use Web Methods or Http Handlers. Web Methods might be a bit easier, and are just server side static functions which are decorated with the [WebMethod] attribute.

根据您尝试执行的操作,您可以使用 Web 方法或 Http 处理程序。Web 方法可能更容易一些,并且只是使用 [WebMethod] 属性修饰的服务器端静态函数。

Here's an example:

下面是一个例子:

C#:

C#:

[WebMethod]
public static string SayHello(string name)
{
    return "Hello " + name;
}

ASPX:

ASPX:

<asp:ScriptManager ID="sm" EnablePageMethods="true" runat="server"/>

<script type="text/javascript">
    #(function()
    {
        $(".hellobutton").click(function()
        {
            PageMethods.SayHello("Name", function(result)
            {
                alert(result);
            });
        });
    }
</script>

<input type="button" class="hellobutton" value="Say Hello" />

回答by Dave Ward

If you're using jQuery, as you mentioned, you can use jQuery to call Page Methods directly, without incurring the overhead of MicrosoftAjax.js and the service proxy it generates to enable the PageMethods.MethodName()syntax.

如果您使用 jQuery,正如您所提到的,您可以使用 jQuery 直接调用页面方法,而不会产生 MicrosoftAjax.js 及其生成的服务代理以启用PageMethods.MethodName()语法的开销。

Given a static [WebMethod]decorated method in PageName.aspxthat's called MethodName, this is an example of how you could call it on the client-side:

给定一个被调用的静态[WebMethod]装饰方法,这是一个如何在客户端调用它的例子:PageName.aspxMethodName

$.ajax({
  type: "POST",
  url: "PageName.aspx/MethodName",
  data: "{}",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(msg) {
    // Do something interesting with msg.d here.
  }
});

回答by Mikael ?stberg

You can use Page Methods, as they are called.

您可以使用页面方法,因为它们被称为。

They are essentially methods on a Page, but are declared as static.

它们本质上是 a 上的方法Page,但被声明为静态的。

public class MyPage : System.Web.UI.Page
{
   [WebMethod]
   public static string Reverse(string message) {
      return message.Reverse();
   }
}

They can then be used like this from client scripting:

然后可以从客户端脚本中像这样使用它们:

function reverseMyString(message) {
   // Magically generated code appears
   PageMethods.SendForm(message, OnSucceeded, OnFailed);
}

function OnSucceeded(result) { alert(result) }
function OnFailed(error) { alert(error.get_message()) }

They are pretty neat compared to asmx web services since they can stay within the same Web Forms functionality that a particular page builds up.

与 asmx Web 服务相比,它们非常简洁,因为它们可以保留在特定页面构建的相同 Web 窗体功能中。

回答by Kon

There sure is a way. Check out this answer. That particular example uses MVC on the back-end, but you can similarly do it with basic ASP.NET - Just look into calling PageMethods/WebMethods via AJAX.

肯定有办法。看看这个答案。该特定示例在后端使用 MVC,但您也可以使用基本的 ASP.NET 进行类似的操作 - 只需查看通过 AJAX调用PageMethods/WebMethods 即可

One of the keys is to declare a static method with the WebMethod attribute on it.

关键之一是声明一个带有 WebMethod 属性的静态方法。