|
-
Jul 3rd, 2009, 05:46 PM
#1
Thread Starter
Frenzied Member
AJAX Timer Keeps Looping/PostBack problem
Hi,
I'm trying to create a 2 second Delay before I initiate a File Download Link.
The problem is, it all works but once the Save Dialog appears, then Timer is supposed to TURN OFF. But then it keeps on calling Page_Load event again, which enables the timer. So it keeps sending/showing the File Save Dialog constantly in a loop.
The thing is it seems to work when I test it on the Development machine, but when uploaded on the server it goes back to a loop.
I have this to try & prevent the loop:
Basically its a label which holds the full URL once its initialised. But empty on form_Load.
If Not lblFileURL.Text = vbNullString Then
Timer1.Enabled = False
Exit Sub
End If
How Do I prevent this. It is a PostBack problem I believe.
Last edited by some1uk03; Jul 3rd, 2009 at 05:52 PM.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 4th, 2009, 11:06 AM
#2
Re: AJAX Timer Keeps Looping/PostBack problem
Hey,
This sounds like it could be one of those situations where you need to check for whether or not the page is actually getting posted back or whether this is the first time the page is loaded.
Are you already using the Page.IsPostBack? If not, you might want to think about using it.
If this doesn't solve the problem, then is it possible for you to post some more of your code?
Gary
-
Jul 4th, 2009, 06:39 PM
#3
Thread Starter
Frenzied Member
Re: AJAX Timer Keeps Looping/PostBack problem
If I ad a If IsPostBack statement, then doesnt even initialise once.
The psuedo is:
On form load, open the database and get a full link.
After 2 seconds a timer is initialized and sends the LINK
Go update database.
Timer is disabled (Timer1.enabled = false)
(save as Dialog appears)
This is where it should all end, but instead, it goes back to Form_Load and the whole loop begins again....
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 5th, 2009, 04:47 AM
#4
Re: AJAX Timer Keeps Looping/PostBack problem
Hey,
Is it possible that you can post the code that you are currently using?
It is going to be difficult to help you without seeing what you are currently using.
Gary
-
Jul 5th, 2009, 12:12 PM
#5
Thread Starter
Frenzied Member
Re: AJAX Timer Keeps Looping/PostBack problem
Some coding:
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'//If the label already contains text, that means its PostBack: Stop The Timer
If Not lblFileURL.Text = vbNullString Then
Timer1.Enabled = False
Exit Sub
End If
Timer1.Enabled = True
End Sub
Protected Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
PrepareFile() 'Gets the Full Link from the Database
Call GoGetTheFile() 'Shows the Save As dialog via
'Response.ContentType = contentType
'Response.AppendHeader("Content-Disposition", "attachment; filename=" & tmpName)
'Response.TransmitFile(Server.MapPath("~/" & txtTheLink.Value))
'Response.End()
Call UpdateDatabase()
lblFileURL.Text = Request.Url.AbsolutePath
'It all SHOULD here! but instead Page_Load is called again
and so thesame process is looped so you get unlimited Save As dialog boxes!
End Sub
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 5th, 2009, 12:21 PM
#6
Re: AJAX Timer Keeps Looping/PostBack problem
Hey,
Okay, a question that I meant to ask earlier was, why are you trying to introduce this 2 second delay? It seems a little unintuitive to want to do this.
Also, can you explain a little bit more about how the user ends up at this page? For instance, are they re-directed to this page, where they have to enter some details, and then the page is posted with what they have entered?
Or, are they re-directed to this page, and then you want a 2 second delay?
Based on the answers to the above, I think we should be able to get something to work for you.
Gary
-
Jul 5th, 2009, 01:21 PM
#7
Thread Starter
Frenzied Member
Re: AJAX Timer Keeps Looping/PostBack problem
The user is given a Link i.e: www.mywebsite.com/95100/default.aspx
So they don't fill a form etc...
That 95100 is a pointer and actually contains a direct link which is called from the database.
But before the Save Ad dialog appears, I want there to be a 2 sec delay.
So page loads.
After 2 secs timer is initialized. Then the full link appears and shows the Save As Dialog.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 5th, 2009, 02:08 PM
#8
Re: AJAX Timer Keeps Looping/PostBack problem
In which case, you should be able to do something like this:
Code:
If Page.IsPostBack Then
Timer1.Enabled = False
Else
Timer1.Enabled = True
End If
That way, when the page first loads, the timer is enabled, then when the page gets posted, the timer is disabled.
Is this what you previoulsy tried?
Gary
-
Jul 5th, 2009, 02:33 PM
#9
Thread Starter
Frenzied Member
Re: AJAX Timer Keeps Looping/PostBack problem
If Page.IsPostBack Then
Timer1.Enabled = False
Else
Timer1.Enabled = True
End If
Doesn't work.. still same result
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 5th, 2009, 02:35 PM
#10
Re: AJAX Timer Keeps Looping/PostBack problem
Hmmm, I would have thought that should do it, but to be fair, I am not in front of my own computer, so I can't double check for you.
The only other thing I can suggest just now (until I get in front of my own computer) is that you try stepping through the code in debug mode, this should hopefully give you some idea of what is going on, and why the page is reloading.
Gary
-
Jul 5th, 2009, 03:07 PM
#11
Thread Starter
Frenzied Member
Re: AJAX Timer Keeps Looping/PostBack problem
Ok, Ive been debugging and stepping through the code. And the result is quite interesting!
I realised that when debugging in my working machine, the Loop does not occur!
BUT ONLY
once uploaded on to the website/server it still the same issue.
Now the question here is why is the server or Development machine giving different results than it should?
I figured out that When the Timer is initialised (i.e Timer1.enabled = True) then Page_Load is called again!
which in turn calls timer1.enabled = True
which in turn call Page_Load event
which in turn calls timer1.enabled = True
which in turn call Page_Load event
So it goes in a loop!
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 5th, 2009, 03:38 PM
#12
Re: AJAX Timer Keeps Looping/PostBack problem
Hey,
Is there a significant difference between the development and production machine? It could be that there is a "race" going on between the logic, introducing timers in this scenario can cause problems.
How about extending the time of the timer to see if that makes any difference. For instance, make it 10 seconds.
Gary
-
Jul 5th, 2009, 04:15 PM
#13
Thread Starter
Frenzied Member
Re: AJAX Timer Keeps Looping/PostBack problem
Is there a significant difference between the development and production machine?
No Idea how the server is setup.
I also tried the 10 sec delay on the timer interval, but same thing!
On timer initialise the form_load event fires!
I've been searching for ways to detect page refreshes, but it all leads to 3rd party .dlls or scripts etc..
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 6th, 2009, 04:07 PM
#14
Thread Starter
Frenzied Member
Re: AJAX Timer Keeps Looping/PostBack problem
I realised something else too:
It seems like a few Subs wont get called because of the code below (which shows the Save As dialog):
I've added messages boxes that popup up at random places within the subs and AGAIN
on the development machine its all fine... but on the server the Message boxes don't show up...
But if I delete the code below, then they do show up. So it seems this is stopping some processing, when it shouldn't!
Code:
Response.ContentType = contentType
Response.Clear()
Response.BufferOutput = True
Response.AppendHeader("Content-Disposition", "attachment; filename=" & tmpName)
Response.TransmitFile(Server.MapPath("~/" & txtTheLink.Value))
Response.Flush()
Another major issue is that since this is stopping some processing, the page is never fully rendered! I get a complete blank white page and only the Save As dialog pops up!
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 6th, 2009, 06:47 PM
#15
Re: AJAX Timer Keeps Looping/PostBack problem
Instead of using a timer serverside can you just do a simple javascript timer on the client?
Personally I think timers are for win form apps, I don't think you should be running a timer on the server, they don't really suit client/server set up.
-
Jul 7th, 2009, 02:53 AM
#16
Re: AJAX Timer Keeps Looping/PostBack problem
Hey,
Once you write out to the Response Stream like that, you won't be able to do anything else. The idea is that you write only the file to the response stream to get the Save As Dialog.
I would suggest you taking a step back and deciding exactly what you need to achieve, and whether the 2 second delay is actually required. Also take brin's suggestion into account.
Gary
-
Jul 7th, 2009, 11:30 AM
#17
Thread Starter
Frenzied Member
Re: AJAX Timer Keeps Looping/PostBack problem
Instead of using a timer serverside can you just do a simple javascript timer on the client?
Well I don't really know javascript myself, so any help on that direction would be appreciated. Anything as long as it gets the job done.
Once you write out to the Response Stream like that, you won't be able to do anything else. The idea is that you write only the file to the response stream to get the Save As Dialog.
Is there another way of writing the response so that it wont hang?
The basic thing I am trying to achieve here is say simply when you visit the Download.com website and are about to download something and it says..
Your download will begin in a moment or click here for the direct link.
Example: Click here
Now you see how the Save As dialog appears after a few seconds. That's what I'm after.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 7th, 2009, 11:32 AM
#18
Re: AJAX Timer Keeps Looping/PostBack problem
Hey,
I guess you could always direct the Response Stream to a hidden iframe on the page so that that takes care of the Save As Dialog, leaving you free to alter the main page with other stuff.
Gary
-
Jul 7th, 2009, 01:10 PM
#19
Thread Starter
Frenzied Member
Re: AJAX Timer Keeps Looping/PostBack problem
I guess you could always direct the Response Stream to a hidden iframe on the page so that that takes care of the Save As Dialog,
And how would you do that ?
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 7th, 2009, 04:43 PM
#20
Re: AJAX Timer Keeps Looping/PostBack problem
Off the top of my head, without the IDE open, it would go something like this.
Create a page called something like download.aspx. The sole purpose of this page is that on the Page_Load event, put the code to redirect the file to the response stream to prompt for the Save As Dialog.
On the page that you are using just now, add an iFrame html element. This is essentially a page within a page. Change the style of the iFrame so that it is invisible. On the client on load event, use a timer, as suggested (if you google for this I am sure you will find a solution quickly) and then set the "src" or "location" property of the iFrame (I forget which one it is, it is getting late) to be the download.aspx page.
What this will do is, the main page you have just now, i.e. the one that says your download will start in two seconds will load, and then the source of the iFrame will be updated to point at the download.aspx which will in turn cause the Save As Dialog to appear. The benefit is that you can then easily control the "direct download link" from the main page, as you are not tied to the response being consumed by the Save AS Dialog.
I hope that makes sense.
Gary
-
Jul 9th, 2009, 11:32 AM
#21
Thread Starter
Frenzied Member
Re: AJAX Timer Keeps Looping/PostBack problem
Good suggestion getp13.
I figured it out using an iFrame. It all works nicely, except that if I set the iFrame to visible=False
Then the Save/Download Dialog never gets launched.
I also pass on the src dynamically (if that makes any diff)
Any way round this visibility issue?
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.

-
Jul 9th, 2009, 01:20 PM
#22
Re: AJAX Timer Keeps Looping/PostBack problem
Hey,
I must be mis-remembering how I got this to work.
How about rather thann set it to invisible, set the height and width to 0? That should do the trick.
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|