Response.redirect inside a Timer Event

G

Guest

Hi everybody,
I'm using a system.timers.timer object like this:

Dim aTimer As New System.Timers.Timer()

In my page_load event I use this:
aTimer.Interval = 5000
aTimer.Enabled = True
AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
.....


Private Sub OnTimedEvent(ByVal source As Object, ByVal e As
System.Timers.ElapsedEventArgs)


response.redirect(....)

End Sub.


Well, I recieved an error in response.redirect sentence,
"response is not available in this context".

I can see that httpcontext.current is nothing and I don't know why.

If I use server.transfer instead of response, I get the same error.


How could I do this without using javascript either Meta Tags?
Why do it happen this?

Regards,

Alberto.
 
G

Guest

Sorry,

But you will have to use client side script (with that timer, the page
will be already freed):

Something like:

protected void Page_Load(object sender, EventArgs e)
{

string destinationURL = "http://www.mysite.com"
Response.AppendHeader("REFRESH", "5;URL=" + destinationURL);
}


You can use as well a javascript solution with a timer, on
registerstartupscript.
--
/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------
 
M

Mark Rae [MVP]

I can see that httpcontext.current is nothing and I don't know why.
If I use server.transfer instead of response, I get the same error.
Why do it happen this?

WebForms (i.e. ASP.NET) apps are not the same as WinForms apps. Because of
the fundamental architecture of the web, they are stateless. This means that
they work in a request / response scenario - a client sends an HttpRequest
to a webserver, the webserver process the HttpRequest and sends back an
HttpResponse. After the HttpResponse has been sent down to the client,
nothing else happens between server and client until / unless the client
sends back another HttpRequest.

When a client makes a request to an aspx page, the Page object goes through
a predefined lifecycle firing events such as Page_Load etc. The very end of
this predefined lifecycle involves sending the HttpResponse down to the
client and then unloading the page. Once the page is unloaded, it's gone...
How could I do this without using javascript either Meta Tags?

Timers in ASP.NET are hugely problematic, and are almost always the wrong
answer. You could, I suppose, create a Timer object and store in Application
cache, but of course it would then stop when the final session times out. To
prevent this, you could keep accessing the website every 15 minutes or so
just to keep it alive...

What are you actually trying to achieve with this timer...?
 
G

Guest

Thanks Braulio, but it's more complicated...

I have a .aspx page with a <IFRAME> tag inside which source connect to other
server.

My timer is on the .aspx page, and I need to wait until a value exist in my
database, so I use the timer event to search in the database for this value
each 5 seconds.

In the moment I get this value, I need to do the redirection, not before,
so, your solution is not valid for me, because I don't know how many time I
need to wait for the value.

Any idea?

Thanks.
 
G

Guest

First of all, thanks Mark,

I'll try to explain you what I want to do.

I have an .aspx page with an <IFRAME> tag inside. The Source attribute of
this tag connect to an external server and this server will insert a value in
a database.

I need to wait until this value appear, but I don't know when it will happen
(but I'm sure it will), so I use the timer to search in the database each
five seconds, and when I get to catch the value, I have to do the
response.redirect, not before.

Any idea how can I do that?
 
M

Mark Rae [MVP]

I'll try to explain you what I want to do.

I have an .aspx page with an <IFRAME> tag inside. The Source attribute of
this tag connect to an external server and this server will insert a value
in
a database.

I need to wait until this value appear, but I don't know when it will
happen
(but I'm sure it will), so I use the timer to search in the database each
five seconds, and when I get to catch the value, I have to do the
response.redirect, not before.

Firstly, you didn't mention any of the above in your original post...
Any idea how can I do that?

1) Forget the server-side timer approach

2) Set the page inside the iframe to refresh itself using the meta tag

3) Add the following to the Page_Load of the page inside the iframe:

protected void Page_Load(object sender, EventArgs e)
{
bool blnValueExists = <however you check your database for this value);

if (blnValueExists)
{
ClientScript.RegisterStartupScript(GetType(), "redirect",
"parent.location.href='page2.aspx';");
}
}
 
G

Guest

Mmmm...

I would call a web service/page method from javascript to ask if the
database is ready (the best way to achieve this is using AJAX ASP .net)

http://www.tipsdotnet.com/ArticleDetail.aspx?RSSEntryID=1447&area=net&subarea=ASPnet

If you can't you can perform this query using XMLHttpRequest (beware here,
in IE and firefox you would have differetn implementations).

Good luck

/// ------------------------------
/// Braulio Diez
///
/// http://www.tipsdotnet.com
/// ------------------------------
 
G

Guest

Many thanks for your gelp Mark, but I afraid that it doesn't work.

It is even more complecated, I'll try to give you all details:

1.) I have a masterpage with a contentplaceholder.
2.) The contentplaceholder is an .aspx with a <IFRAME> tag (i.e page1.aspx)
3.) The <IFRAME> source is to another .aspx (i.e. page2.aspx)
4.) In OnLoad Event of page2.aspx is built a Form in runtime and it is
submited automatically. The Action of this form connect to an external page.

So, when I see the sourcecode, I see the request to the form submit, that
is, I have no control to this page. The URL is page2.aspx, but the sourcecode
is the request to the form submited.

After the form is submited to an external page, is processed there and if it
is ok, it will insert a value on a database.

Meanwhile, I have to be waiting for this value, but only I have control of
page1.aspx and MasterPage. Then, when the value is inserted I have to
redirect to another page, for that, I was using a timer.

I can't use OnLoad on page2.aspx, because when the form is submited, the
code is rewritten for the external server, so the meta tag disappear and I
can't write any code because I have no control of that.

Do you understand what I mean?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top