Showing posts with label pageload. Show all posts
Showing posts with label pageload. Show all posts

Wednesday, March 28, 2012

pageLoad and pageUnload called when using PageMethods ?

I have pageLoad and pageUnload functions in my page where I do my initialization and cleanup. I also use UpdatePanels and PageMethods. I am noticing that pageLoad and pageUnload methods are getting called on every PageMethod (ajax) call !!.

Doesn't pageLoad and pageUnload correspond to window.load and window.unload events respectively ?

Basically, I want methods which should be called when _entire_ page is loaded or unloaded and should not be called while using PageMethods or postbacks from within UpdatePanel

Please guide.

Regards & thanks

Kapil


Hi ksachdeva17,

I hope i understand your situation correctly. You don't want that the page life cycle will be invoked when there is an event triggered by an action from a control inside your updatepanel (or trigger). Well the situation on the server will be the same when you're using an updatepanel. The async postback will take the same cycle as an synchronous postback. The thing that is different is the rendering of the page. The controls in your updatepanel will only be rendered and not the whole page. I hope this will make things clear for you.

Regards,


Thanks Dennis,

You are saying that window.onload and window.unload (DOM) events will occur if we make XMLHttprequest from the page, I would guess they should not as XMLHttpRequest is independent of browser post !!. I understand that on server side async postback would take the same cycle as synchronous postback but on the page (in client browser) it should not.

Regards

Kapil


Hi Kapil,

I was saying that the page life cycle on the server will stay the same no matter if it's a synchronous of asynchronous postback. I found this in the documentation of Ajax.

Client Page Life-cycle Events

During ordinary page processing in the browser, thewindow.onload DOM event is raised when the page first loads. Similarly, thewindow.onunload DOM event is raised when the page is refreshed or when the user moves away from the page.

However, these events are not raised during asynchronous postbacks. To help you manage these types of events for asynchronous postbacks, the PageRequestManager class exposes a set of events. These resemblewindow.load and other DOM events, but they also occur during asynchronous postbacks. For each asynchronous postback, all page events in the PageRequestManager class are raised and any attached event handlers are called.

Hope this helps!

Regards,


Thanks Dennis,

So my inference is that pageLoad is not same as window.onload. If you could please verify my understanding. I think I should be able to add the event handler using $addHandler with 'load' as an event name to execute things when page is loaded for the first time (no async) . Correct ?

Regards

Kapil


HI Kapil,

PageLoad and window.load are not the same indeed. you can handle also the logic you want in you code behind using thefollowing funciton:

if

(!ScriptManager.GetCurrent(this).IsInAsyncPostBack)

{

myTextBox.Text =

"Test";

}

Probably you already see what it's doing. It will check if it's in a asynchronous postback, when you want to handle some functionality when the page is in a synchronous postback you can you the statement above.

It is also possible to add a handeler to the load event of your window! like you suggested

Good luck

Regards,

pageLoad javascript function not being called

for some reason the javascript pageLoad method is not being called

the javascript is in scirbble.js

the example i have followed informs me that the pageLoad method should be ran when the atlas script manager control has loaded.

Is this corret.

Could someone please point me in the right direction for getting the below code to work

Below is the javascript and aspx code

function pageLoad()
{
var surface = document.getElementById("drawingSurface");
image = surface.getElementsByTagName("IMG")[0];
originalSrc = image.src;

surface.attachEvent("onmousedown", startStroke);
surface.attachEvent("onmouseup", endStroke);
surface.attachEvent("onmouseout", endStroke);
surface.attachEvent("onmousemove", addPoints);
}

form id="form1" runat="server">
<Atlas:ScriptManager ID="AtlasScriptManager" runat="server" >
<Services>
<Atlas:ServiceReference Path="ScribbleService.asmx" />
</Services>
<Scripts>
<Atlas:ScriptReference Path="ScriptLibrary\Scribble.js" />
</Scripts>
</Atlas:ScriptManager>
<div id="drawingSurface"
style="border:solid 1px black;height:200px;width:200px">
<img alt="Scribble" src="http://pics.10026.com/?src=ScribbleImage.ashx"
style="height:200px;width:200px" />
</div>
</form>

i have the same problem since the last release. I had pageLoad() methods which were able to set elements on the initial loading which no longer fire automatically :?(

As a workaround, you can attach to thePageRequestManager events like so :

<script type="text/javascript"> Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoad);</script>

when i do the above mentioned work around i get an error

'Sys.WebForms.PageRequestManager' is null or not an object

i have tried adding the script above and below the sript manager html elements but it makes no diffrence.

below is the aspx page

any help would glady be appreciated.

<headrunat="server">

<title>Atlas Scribble Sample</title>

</

head><body><formid="form1"runat="server"><Atlas:ScriptManagerID="AtlasScriptManager"runat="server"EnablePartialRendering=true><Services><Atlas:ServiceReferencePath="ScribbleService.asmx"/></Services><Scripts><Atlas:ScriptReferencePath="ScriptLibrary\Scribble.js"/></Scripts></Atlas:ScriptManager><divid="drawingSurface"style="border:solid 1px black;height:200px;width:200px"><imgalt="Scribble"src="ScribbleImage.ashx"style="height:200px;width:200px"/></div><scripttype="text/javascript">

Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoad);

</script></form>

</

body>

</

html>
throw an alert into your pageLoad() function to see if it's getting called; my guess is that it is, at least nothing you've posted here would prevent it. The issue I see is that the element 'surface' isn't going to respond to the attachEvent function as you have it written, at least not in this framework. You could try to us $addHandler(surface,'onmousedown',startStroke); instead.

pageLoad fires before script references are loaded

I have an aspx page with:

- a scriptmanager that has 2 script references. Both scripts have the "if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();" line at their end.

- a script section with a pageLoad function that calls a function defined in one of the 2 script references.

- an iframe (with src set to about:blank).

Result:

I get an "object undefined" error in the pageLoad function. with fiddler and in the debugger I can see that my script rerefences have not yet been loaded. As fas as I know, they should! It always worked until we added the iframe control. Removing the iframe control resolves the problem. To me this looks like an error in ajax where the pageLoad is executed on the load of the iframe contents instead of on the load of the main page.

Anyone any suggestion?

Could you share some code that reproduces this bug? I was unable to get the behavior you describe.

Are you using a function named pageLoad? Or Sys.Application.add_load(...)? Or Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(...)? Code would help a lot.


Thanks for looking into the problem. I will try to assemble a minimal solution that exposes the bug. BTW: it is a pageLoad function. In the debugger I saw that Ajax in the onload event looks if it exists and, if so, calls it. That's also how I found out that it gets called twice from this event handler. Give me some time to assemble the solution.

Thanks agian,

Erwin

pageLoad() javascript function

I understand that a script with the pageLoad() function is a way to hook into the client side load event.

That works great if you are working in the .aspx page itself. What if you're in a user control and want something to hook into the pageLoad() event on the client?

I could use Page.RegisterClientScriptBlock() to inject the JS into the page, but that would overwrite any other function that might be in the master page, content page or other user controls.

Isn't there a way to just add an event to the page load on the client side and not worry about conflicting with other code?

Thanks,

John

Can you give an example of what you're trying to do?


I'm not sure I understand your problem, but if your user control inherits from Sys.UI.Control it's initialize method will be called when the page loads.

You can add your own pageLoad() handler by usingSys.Application.add_load().


HI,

I could use the page_load server event, but that would delay the rendering of the entire page until this process is complete. The effect I am looking for is to render a page so the user has something to look at. Then, when theclient on_load event fires, initiate some long running tasks that can take place while the user already has something to look at.

Thanks,

John


Sys.Application.Load is the client side event, not server side. Take a look at the documentation I linked. That's the event that pageLoad() calls implicitly.


Hi,

In the User control , write this function ,
<script>
function IAMIntheUserControl(object,eventArgs) {
}
</script>

register this script with RegisterClientScriptBlock.

Sys.Application.add_load( IAMIntheUserControl );