Response.redirect inside a Timer Event

Discussion in 'ASP .Net' started by =?Utf-8?B?YWxiZXJ0b3Nvcmlh?=, Oct 23, 2007.

  1. 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.
     
    =?Utf-8?B?YWxiZXJ0b3Nvcmlh?=, Oct 23, 2007
    #1
    1. Advertising

  2. 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.
    >
    >
    >
    >
    >
    >
     
    =?Utf-8?B?QnJhdWxpbyBEaWV6?=, Oct 23, 2007
    #2
    1. Advertising

  3. "albertosoria" <> wrote in message
    news:...

    > 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
     
    Mark Rae [MVP], Oct 23, 2007
    #3
  4. 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.
    > >
    > >
    > >
    > >
    > >
    > >
     
    =?Utf-8?B?YWxiZXJ0b3Nvcmlh?=, Oct 23, 2007
    #4
  5. 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" <> wrote in message
    > news:...
    >
    > > 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
    >
    >
     
    =?Utf-8?B?YWxiZXJ0b3Nvcmlh?=, Oct 23, 2007
    #5
  6. "albertosoria" <> wrote in message
    news:...

    > 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
     
    Mark Rae [MVP], Oct 23, 2007
    #6
  7. 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
    /// ------------------------------




    "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.
    > > >
    > > >
    > > >
    > > >
    > > >
    > > >
     
    =?Utf-8?B?QnJhdWxpbyBEaWV6?=, Oct 23, 2007
    #7
  8. 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" <> wrote in message
    > news:...
    >
    > > 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
    >
    >
     
    =?Utf-8?B?YWxiZXJ0b3Nvcmlh?=, Oct 23, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman
    Replies:
    2
    Views:
    8,092
    Charles A. Lackman
    Jan 11, 2006
  2. =?Utf-8?B?VHJveQ==?=

    Response.redirect does not redirect from .aspx page

    =?Utf-8?B?VHJveQ==?=, Oct 18, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    5,362
    Srowe1975
    Oct 15, 2008
  3. Simon

    page timer, or redirect timer

    Simon, Nov 4, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    11,262
    Bruce Barker
    Nov 4, 2005
  4. Sal
    Replies:
    1
    Views:
    1,006
  5. Ron Howard
    Replies:
    2
    Views:
    577
    Ron Howard
    Aug 11, 2004
Loading...

Share This Page