<p>I'm looking for a way to check within <code>pageLoad()</code> if this method is raised during load event because of a postback/async postback or because of being loaded and access the first time.</p>

<p>This is similar to <code>Page.IsPostback</code> property within code behind page.</p>

<p>TIA,
Ricky</p>

[{'answer_id': 40924, 'author': 'Greg Hurlman', 'author_id': 35, 'author_profile': 'https://Stackoverflow.com/users/35', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>That said, I really hope that there's a client-only solution for this.</p>\n\n<p><strong>Edit:</strong> @mmattax - I believe he's looking for a client-side solution - the JavaScript equivalent of that.</p>\n"}, {'answer_id': 40925, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 2, 'selected': False, 'text': '<p>What you can do is wire up to the load event of the <a href="http://msdn.microsoft.com/en-us/library/bb310856.aspx" rel="nofollow noreferrer">Sys.Application</a> class. you can then use the isPartialLoad property of the <a href="http://msdn.microsoft.com/en-us/library/bb397457.aspx" rel="nofollow noreferrer">Sys.ApplicationLoadEventArgs</a> class. I believe that would let you know if you are in a async postback or not.</p>\n\n<p>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.</p>\n'}, {'answer_id': 40955, 'author': 'Shawn', 'author_id': 26, 'author_profile': 'https://Stackoverflow.com/users/26', 'pm_score': 0, 'selected': False, 'text': '<p>You can still use Page.IsPostback during an async call.</p>\n'}, {'answer_id': 40981, 'author': 'Ricky Supit', 'author_id': 4191, 'author_profile': 'https://Stackoverflow.com/users/4191', 'pm_score': 2, 'selected': False, 'text': '<p>@Darren: Thanks for the answer. I had tried to create pageLoad with event argument <a href="http://asp.net/AJAX/Documentation/Live/ClientReference/Sys/ApplicationLoadEventArgsClass/ApplicationLoadEventArgsIsPartialLoadProperty.aspx" rel="nofollow noreferrer">ApplicationLoadEventArgs</a> as parameter (see below).  However according to <a href="http://msdn.microsoft.com/en-us/library/bb386417.aspx" rel="nofollow noreferrer">this</a>:</p>\n\n<p><em>The load event is raised for all postbacks to the server, which includes asynchronous postbacks.</em></p>\n\n<p>As you have indicated, the isPartialLoad property does not cover all postback scenarios.  It\'ll be nice if the event argument also contain isPostback property.</p>\n\n<pre><code>   function pageLoad(sender, arg) {\n      if (!arg.get_isPartialLoad()) {\n          //code to be executed only on the first load\n      }\n   }\n</code></pre>\n\n<p>@mmattax: I\'m looking for property that can be called from client-side (javascript).</p>\n'}, {'answer_id': 44993, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 0, 'selected': False, 'text': '<p><a href="http://www.asp.net/AJAX/documentation/live/ClientReference/Sys/ApplicationClass/SysApplicationInitEvent.aspx" rel="nofollow noreferrer">Application.Init</a> is probably a more appropriate event to use, if you only want the code to execute on the first load.</p>\n'}, {'answer_id': 45117, 'author': 'Ricky Supit', 'author_id': 4191, 'author_profile': 'https://Stackoverflow.com/users/4191', 'pm_score': 0, 'selected': False, 'text': '<p>@Dave Ward: This normally would work. 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.</p>\n\n<p>It will be nice if there is PostInit event.</p>\n'}, {'answer_id': 45189, 'author': 'Dave Ward', 'author_id': 60, 'author_profile': 'https://Stackoverflow.com/users/60', 'pm_score': 4, 'selected': True, 'text': '<p>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:</p>\n\n<pre><code>Sys.Application.add_init(AppInit);\n\nfunction AppInit() {\n  Sys.Application.add_load(RunOnce);\n}\n\nfunction RunOnce() {\n  // This will only happen once per GET request to the page.\n\n  Sys.Application.remove_load(RunOnce);\n}\n</code></pre>\n\n<p>That will execute after Application.Init.  It should be the last thing before pageLoad is called.</p>\n'}, {'answer_id': 46319, 'author': 'Ricky Supit', 'author_id': 4191, 'author_profile': 'https://Stackoverflow.com/users/4191', 'pm_score': 0, 'selected': False, 'text': "<p>@Dave Ward:\nThe use of RunOnce method works perfectly.  This solve my problem without having the workaround to check first if handler already exist before attaching to an event.</p>\n\n<p>I'll mark your answer as an accepted answer.  Thanks again.</p>\n"}, {'answer_id': 75248, 'author': 'Compulsion', 'author_id': 3675, 'author_profile': 'https://Stackoverflow.com/users/3675', 'pm_score': 0, 'selected': False, 'text': '<p>Here\'s our Ajax equivalent to isPostback which we\'ve been using for a while.</p>\n\n<pre><code>public static bool isAjaxRequest(System.Web.HttpRequest request)\n    {//Checks to see if the request is an Ajax request\n        if (request.ServerVariables["HTTP_X_MICROSOFTAJAX"] != null ||\n            request.Form["__CALLBACKID"] != null)\n            return true;\n        else\n            return false;\n    }\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/40912', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4191/']