Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Response.redirect inside a Timer Event

Reply
Thread Tools

Response.redirect inside a Timer Event

 
 
=?Utf-8?B?YWxiZXJ0b3Nvcmlh?=
Guest
Posts: n/a
 
      10-23-2007
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.






 
Reply With Quote
 
 
 
 
=?Utf-8?B?QnJhdWxpbyBEaWV6?=
Guest
Posts: n/a
 
      10-23-2007
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
/// ------------------------------




"albertosoria" wrote:

> 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.
>
>
>
>
>
>

 
Reply With Quote
 
 
 
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      10-23-2007
"albertosoria" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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...?


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
=?Utf-8?B?YWxiZXJ0b3Nvcmlh?=
Guest
Posts: n/a
 
      10-23-2007
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.


"Braulio Diez" wrote:

> 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
> /// ------------------------------
>
>
>
>
> "albertosoria" wrote:
>
> > 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.
> >
> >
> >
> >
> >
> >

 
Reply With Quote
 
=?Utf-8?B?YWxiZXJ0b3Nvcmlh?=
Guest
Posts: n/a
 
      10-23-2007
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?



"Mark Rae [MVP]" wrote:

> "albertosoria" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > 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...?
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net
>
>

 
Reply With Quote
 
Mark Rae [MVP]
Guest
Posts: n/a
 
      10-23-2007
"albertosoria" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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';");
}
}


--
Mark Rae
ASP.NET MVP
http://www.markrae.net

 
Reply With Quote
 
=?Utf-8?B?QnJhdWxpbyBEaWV6?=
Guest
Posts: n/a
 
      10-23-2007
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/ArticleDet...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
/// ------------------------------




"albertosoria" wrote:

> 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.
>
>
> "Braulio Diez" wrote:
>
> > 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
> > /// ------------------------------
> >
> >
> >
> >
> > "albertosoria" wrote:
> >
> > > 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.
> > >
> > >
> > >
> > >
> > >
> > >

 
Reply With Quote
 
=?Utf-8?B?YWxiZXJ0b3Nvcmlh?=
Guest
Posts: n/a
 
      10-23-2007
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?


"Mark Rae [MVP]" wrote:

> "albertosoria" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > 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';");
> }
> }
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I know about the shutdown timer, is there a "switch on" timer... Mal Carter Computer Support 3 01-12-2008 01:58 AM
page timer, or redirect timer Simon ASP .Net 1 11-04-2005 07:14 PM
System.Timers.Timer/System.Threading.Timer Not Firing At All on Dev Server james.e.coleman@gmail.com ASP .Net 1 02-22-2005 09:41 PM
rendering Button inside Render() event, makes it loose its click event handler association sonic ASP .Net 1 01-07-2005 06:33 PM
System.Timers.Timer vs. System.Threading.Timer Kelsang Wangchuk ASP .Net 0 07-31-2003 04:28 PM



Advertisments