如何在asp.net webforms中使用ajax

时间:2022-05-06 03:21:22

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而不必遍历页面生命周期?

4 个解决方案

#1


7  

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直接调用Page Methods,而不会产生MicrosoftAjax.js及其生成的服务代理的开销,以启用PageMethods.MethodName()语法。

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

给定一个名为MethodName的PageName.aspx中的静态[WebMethod]修饰方法,这是一个如何在客户端调用它的示例:

$.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.
  }
});

#2


8  

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 Handler。 Web方法可能更容易一些,只是服务器端静态函数,它们使用[WebMethod]属性进行修饰。

Here's an example:

这是一个例子:

C#:

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

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" />

#3


2  

You can use Page Methods, as they are called.

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

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

它们本质上是页面上的方法,但声明为静态。

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窗体功能相同。

#4


1  

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属性的静态方法。

#1


7  

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直接调用Page Methods,而不会产生MicrosoftAjax.js及其生成的服务代理的开销,以启用PageMethods.MethodName()语法。

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

给定一个名为MethodName的PageName.aspx中的静态[WebMethod]修饰方法,这是一个如何在客户端调用它的示例:

$.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.
  }
});

#2


8  

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 Handler。 Web方法可能更容易一些,只是服务器端静态函数,它们使用[WebMethod]属性进行修饰。

Here's an example:

这是一个例子:

C#:

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

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" />

#3


2  

You can use Page Methods, as they are called.

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

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

它们本质上是页面上的方法,但声明为静态。

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窗体功能相同。

#4


1  

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属性的静态方法。