简体   繁体   English

如何检查页面是否在ASP.NET AJAX上的保留函数pageLoad中回发

[英]How to check if page is postback within reserved function pageLoad on ASP.NET AJAX

I'm looking for a way to check within pageLoad() if this method is raised during load event because of a postback/async postback or because of being loaded and access the first time. 我正在寻找一种方法来检查pageLoad()是否在加载事件期间由于回发/异步回发或由于第一次加载和访问而引发此方法。

This is similar to Page.IsPostback property within code behind page. 这类似于代码隐藏页面中的Page.IsPostback属性。

TIA, Ricky TIA,Ricky

One way you could do that is to wire up an Application.Load handler in Application.Init, then have that handler unbind itself after running: 您可以这样做的一种方法是在Application.Init中连接Application.Load处理程序,然后让该处理程序在运行后取消绑定:

Sys.Application.add_init(AppInit);

function AppInit() {
  Sys.Application.add_load(RunOnce);
}

function RunOnce() {
  // This will only happen once per GET request to the page.

  Sys.Application.remove_load(RunOnce);
}

That will execute after Application.Init. 这将在Application.Init之后执行。 It should be the last thing before pageLoad is called. 它应该是调用pageLoad之前的最后一件事。

@Darren: Thanks for the answer. @Darren:谢谢你的回答。 I had tried to create pageLoad with event argument ApplicationLoadEventArgs as parameter (see below). 我曾尝试用事件参数ApplicationLoadEventArgs作为参数创建pageLoad(见下文)。 However according to this : 但是根据这个

The load event is raised for all postbacks to the server, which includes asynchronous postbacks. 针对服务器的所有回发都会引发load事件,其中包括异步回发。

As you have indicated, the isPartialLoad property does not cover all postback scenarios. 如您所示,isPartialLoad属性未涵盖所有回发方案。 It'll be nice if the event argument also contain isPostback property. 如果event参数还包含isPostback属性,那将会很好。

   function pageLoad(sender, arg) {
      if (!arg.get_isPartialLoad()) {
          //code to be executed only on the first load
      }
   }

@mmattax: I'm looking for property that can be called from client-side (javascript). @mmattax:我正在寻找可以从客户端(javascript)调用的属性。

You could have a hidden input that you set to a known value on the server side if it's a postback/callback - and your javascript could check that value. 你可以有一个隐藏的输入,如果它是回发/回调你在服务器端设置为已知值 - 你的javascript可以检查该值。

That said, I really hope that there's a client-only solution for this. 也就是说,我真的希望有一个仅限客户端的解决方案。

Edit: @mmattax - I believe he's looking for a client-side solution - the JavaScript equivalent of that. 编辑: @mmattax - 我相信他正在寻找一个客户端解决方案 - 相当于JavaScript的JavaScript。

What you can do is wire up to the load event of the Sys.Application class. 您可以做的是连接到Sys.Application类的load事件。 you can then use the isPartialLoad property of the Sys.ApplicationLoadEventArgs class. 然后,您可以使用Sys.ApplicationLoadEventArgs类的isPartialLoad属性。 I believe that would let you know if you are in a async postback or not. 我相信如果您处于异步回发状态,我会告诉您。

To know if you are in a post back, you'll have to handle that in server side code and emit that to the client. 要知道您是否在回发帖子,您必须在服务器端代码中处理它并将其发送到客户端。

您仍然可以在异步调用期间使用Page.IsPostback。

如果您只希望代码在第一次加载时执行, Application.Init可能是一个更合适的事件。

@Dave Ward: This normally would work. @Dave Ward:这通常会奏效。 However, the code is to attach event on behavior object. 但是,代码是在行为对象上附加事件。 Because the creation of behavior object happens during Application.Init, attaching to that event will lead to unpredictable behavior. 因为行为对象的创建发生在Application.Init期间,所以附加到该事件将导致不可预测的行为。

It will be nice if there is PostInit event. 如果有PostInit事件会很好。

@Dave Ward: The use of RunOnce method works perfectly. @Dave Ward:使用RunOnce方法非常有效。 This solve my problem without having the workaround to check first if handler already exist before attaching to an event. 这解决了我的问题,没有解决方法在附加到事件之前首先检查处理程序是否已存在。

I'll mark your answer as an accepted answer. 我会将你的答案标记为已接受的答案。 Thanks again. 再次感谢。

Here's our Ajax equivalent to isPostback which we've been using for a while. 这是我们的Ajax相当于isPostback,我们已经使用了一段时间。

public static bool isAjaxRequest(System.Web.HttpRequest request)
    {//Checks to see if the request is an Ajax request
        if (request.ServerVariables["HTTP_X_MICROSOFTAJAX"] != null ||
            request.Form["__CALLBACKID"] != null)
            return true;
        else
            return false;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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