0

Example below is not working to delay a page redirect after 5 sec. - The Header update doesn't seem to work. Is there a pure VB solution for is the header the only way to go? Thought of just running a timer to delay next line of execution?

Tried commented out section as well - still didn't fire the header change.

Dim TF As String = ResetNewPassword(uName, pAnwser, newPassword)
Dim dateUpdated As Boolean = UpdateLastLoginDate(uName)
    If TF Then
        uEmail = u.Email
        Label2.Text = "Users password has been updated"
        Training.xMail(uEmail, Label2.Text, "Password Changed")
        Response.AppendHeader("REFRESH", "5;URL=Default.aspx")

                'Dim meta As New HtmlMeta()
                'meta.HttpEquiv = "Refresh"
                'meta.Content = "5;url=Page2.aspx"
                'Me.Page.Controls.Add(meta)

        PassQuestion1.Text = "You will now be redirected in 5 seconds"
    Else
        Label2.Text = "Users password failed to update please try again!!!"
        Training.Mail("mail_user_acct", Label2.Text, "Password Failed - resetPassword")
    End If
ADyson
  • 57,178
  • 14
  • 51
  • 63
BarclayVision
  • 865
  • 2
  • 12
  • 41
  • Timer won't work because that will just stop the response going back to the browser, so the user will never see the message. A meta refresh tag should do the trick. Or inject some JavaScript with a timeout set – ADyson Oct 09 '19 at 17:13
  • so why is the current code not working? – BarclayVision Oct 09 '19 at 17:15
  • https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections#Alternative_way_of_specifying_redirections lays out your options pretty nicely. N.b. The "refresh" header is a legacy thing from Netscape days as far as I work out from a bit of brief googling. It might work in theory but it's not standard so it's perhaps best not to rely on it. There's no guarantee that browsers will support it, or if they do, that they will continue to support it in future – ADyson Oct 09 '19 at 17:17
  • looking for a solution? Thoughts? – BarclayVision Oct 09 '19 at 17:18
  • Yes. I've given you my two thoughts already (see end of first comment) and also a link which gives examples of them (see start of second comment). Did you read my comments carefully? – ADyson Oct 09 '19 at 17:20
  • JavaScript is not an option, that's why I stated looking for pure VB if possible. sorry if I wasn't clear in my question. – BarclayVision Oct 09 '19 at 17:22
  • If a pure VB solution was possible I would have mentioned it already. You're trying to tell the browser what to do next after you've already delivered the initial response to it. At that time, your VB code on the server has already ended. That's how web apps work. Why is JavaScript not possible? Are you targeting a browser which doesn't allow JavaScript?? – ADyson Oct 09 '19 at 17:23
  • yes - javascript is restricted – BarclayVision Oct 09 '19 at 17:24
  • Java is not JavaScript. They are totally different. If you disallow JavaScript then 99% of modern web pages will not work properly, probably including your asp.net application -.net WebForms relies heavily on JavaScript to help with form submission, viewstate etc. So I would be surprised if it's disabled. Java in the browser is often a security risk and is frequently disabled. But that's not relevant here. – ADyson Oct 09 '19 at 17:26
  • But anyway if you really don't like the JavaScript idea then just use the meta tag instead, that's a HTML-only solution and supported in all browsers – ADyson Oct 09 '19 at 17:26
  • I see we are not getting very far.. It's not that I don't like JavaScript... and the meta example I listed is not working... – BarclayVision Oct 09 '19 at 17:28
  • Your example uses a http header not a meta tag. You've commented out the meta tag. Meta tag will work if you define it properly in the head section of the page – ADyson Oct 09 '19 at 17:29
  • I guess an example would be out of the question... – BarclayVision Oct 09 '19 at 17:34
  • Well what you wrote should be fairly close, but try adding the tag to the header instead of the page, e.g.. `Dim meta As New HtmlMeta() meta.HttpEquiv = "Refresh" meta.Content = "5;URL=Page2.aspx" Me.Header.Controls.Add(meta)` . That's assuming that your head tag has a runat="server" and that the above code in your question is running in a suitable context (e.g. page_load, prerender, or maybe a click event or similar). If you're doing this via update panels or anything like that then it probably won't work. – ADyson Oct 09 '19 at 20:22
  • You can verify the output by viewing the source of the page in your browser (e.g. with Ctrl+U in most browsers) to check the tag is placed and formed correctly. Obviously you hopefully will barely have time for that before it redirects... – ADyson Oct 09 '19 at 20:23
  • P.S. this question has been asked a lot of times before...there are a lot of existing examples (e.g. [here](https://stackoverflow.com/questions/30246939/how-to-redirect-to-another-page-after-a-delay) and [here](https://stackoverflow.com/questions/12219246/submit-show-results-delay-3-seconds-and-redirect) and [here](https://stackoverflow.com/questions/10141504/time-delay-before-redirect), just for starters) as well as similar content on other websites. – ADyson Oct 09 '19 at 20:30
  • Inside an UpdatePanel as well as MasterPage and LoginControl. I have searched numerous post for a resolution, none seem to work for me. – BarclayVision Oct 09 '19 at 20:34
  • Ok so...an UpdatePanel is basically turning your form submission into an AJAX request (in the background, ASP.NET does this by adding a big chunk of JavaScript into your page). So it's not a full postback or page refresh. In this situation adding a meta tag, or setting a response header, is never going to have any effect. The browser does not respond to an AJAX request in the same way it responds to a regular full page refresh. – ADyson Oct 09 '19 at 21:16
  • The whole point of AJAX/update panels is to avoid refreshing the whole page, so it kind of makes less sense to do a redirect following that. Maybe you should just ditch the update panel for this purpose, if you're planning to redirect the user anyhow. – ADyson Oct 09 '19 at 21:17
  • Or alternatively, consider not redirecting the user, but instead displaying whatever content is needed inside the current page rather than on the secondary page. You didn't say what was in the other page, but that's theoretically a possibility - indeed it's one of the advantages of AJAX-based approaches. Maybe that works in this scenario, maybe it doesn't, but it's one option you can consider. – ADyson Oct 09 '19 at 21:21

0 Answers0