views:

67

answers:

3

After x seconds, after the page loads, I need to execute a method in the code behind. I cannot move this logic into JS.

Do I need to use delegates/events for this? Can anyone give me an example (preferably with a code snippet)??

+3  A: 

Put a counter in JS that measures the X seconds. Once it's reached it's mark, have it send a message via AJAX back to the server, and the server executes the method.

That's about the only way to ensure that the counting of those seconds is accurate to when the page finishes loading. If you don't care too much about accuracy, just have the server kick off the method x seconds after it sends the page.

Caladain
Can you elaborate on "just have the server kick off the method x seconds after it sends the page."
Well, your web server sends off pages when clients make requests. I wouldn't recommend it, but i've seen plenty of server-side apps that monitor when a page gets sent off by a request and then do something x seconds after. This is klugy. You're best bet is to use AJAX and fire a message back to the server..this will get you what i think you're after.
Caladain
My website doesn't currently use AJAX and would not like to update a production website if it's absolutely not necessary. I don't need the timing to be accurate, would need to fire something off after 45seconds of page load.
I don't think you're going to get away from needing to update the production website. The easiest, and most bullet proof solution would require a smidget of javascript to count the seconds when it needs to fire and then sending via AJAX (Asynchronous JavaScript and XML, really a group term for a collection of techniques) to the server to do that popup. It sounds much more scarey from a production sense than it really is.
Caladain
Exactly how do you plan to make *any* change without updating production code?
David Lively
Sorry for the confusion. That's not what I meant. I meant 'I would avoid updating a production website with AJAX code if it wasn't absolutely necessary'
A: 

You could try this

System.Threading.Thread.Sleep(3000);

Hope this helps.

Ash Burlaczenko
After the page loads. This will just stall ASP.NET in it's response back to the client.
George
Yes, I don't want no stalling!
+2  A: 

Your best solution is going to be to use javascript to either cause a postback, or to send an AJAX request to the server after the X seconds has elapsed.

Due to the page lifecycle of ASP.NET pages, you can't do it from the code-behind directly. You can see this article for more information on the ASP.NET Page Lifecycle.

I would put a bit of javascript that uses the "SetTimeout" to trigger a JS method call that either does the Ajax request, or forces the postback, depending on what you are doing.

Edit

Based on the additional information you put in the comments to the post i would recommend a modified approach. If all you are doing is launching another window, and you want to delay that logic.

Instead of directly calling the window.open or however you are doing it. Simply put that code inside of the code that would be called using the "SetTimeout" method as I referred to earlier. No need to involve the server-side at all.

Mitchel Sellers