proseidon
proseidon

Reputation: 2305

Does Response.Redirect() kill page functionality?

I've got a simple question. If I'm using a Response.Redirect(), will that kill all page functionality assuming the user cancels the redirect?

For example, pretend I have a page with a button that, when pressed, pops up a message that says "hi". Then, at some point in the code, the user encounters a Response.Redirect(). Before the page is cleared on the browser, they hit STOP in their browser. Then, they click the button. Will it still say "hi", or will the page be non-functional?

EDIT FOR MORE INFORMATION: Maybe I am just not understanding the answers, but I still don't have an answer to my question. Let me rephrase to be more precise.

I have two buttons that both execute a C# function when they are clicked. One of them executes a Response.Redirect() to a different page. The other prints out "Hi" in a message box. The user presses button 1. The response.redirect starts processing, but before the user is redirected they decided that they made a mistake and they hit the STOP button in their browser. The user is still looking at the same web page. They click on button #2. My question is, does the message "Hi" pop up? Does the C# function still work? Or would the code behind be inaccessible after the attempted redirect?

Upvotes: 1

Views: 1069

Answers (4)

nunespascal
nunespascal

Reputation: 17724

Solution
Users on a slow network connection do see the initial page for a little longer. If all you want to do is prevent them from pressing those buttons by mistake, you can create a <div> to cover your entire screen area and grey it out with a semi-transparent image. Users will not be able to click buttons unless they refresh the page.

You can get this effect easily by showing a jQuery modal dialog on form submit, with a message to the user that the data is being saved.

This solution will only prevent the accidental click. If you want to prevent malicious repeated requests, in addition to the modal dialog you should track your application state on the server and indicate that a save has already taken place. A simple session variable should suffice to indicate this.

Technical explaination
The http protocol used by the web server is stateless. Every time you send a request to the server, it builds your Page object to service this request and returns a reply. It will not remember what you send it last time. If you need that, you have to do it yourself.

When you click the "hi" button, in your c# code you set the message. In reply to this request the server replies with a success status message HTTP/1.1 200 OK as the first line of the response. This tells the browser that everything is ok, and it will process the response based on the Content-Type header returned.

When you click the button to redirect to another page. Your browser sends a request to the first page. There when you execute a Response.Redirect your server send back a redirect message to the browser.

HTTP/1.1 302 Found
Location: http://Page2URL

This tells the browser that it has to go to this new location to get a response. The browser now makes a new request to the url mentioned by the location header and gets the response.

The user may abort the request at any of these stages. Since http is stateless, you can resubmit and of the two requests or click on another button and make a new request. The server will dutifully send it to asp.net like nothing ever happened and you will get a response.

Upvotes: 1

Rob Wilkins
Rob Wilkins

Reputation: 1650

Unless the browser has actually redirected, or you've put some specific code in place to invalidate it, then the second event is completely independent of the first. When you click the second button, it sends an entirely new request back to the server, and the fact that the other button was clicked previously will have no effect on it -- unless you have explicitly tracked the event yourself, it won't even be aware that other button was clicked before.

The second button will fire as normal, and the second C# function will fire. You really shouldn't see any difference in the browser between clicking the second button on its own, and clicking the first button, stopping the browser redirecting, then clicking the second.

Upvotes: 1

Dai
Dai

Reputation: 155175

That's not how Response.Redirect() works.

When the user clicks a button (that presumably fires a POST request to the server) then nothing will happen on the client until it receives a response from the server. If the code that generated the response made a call to Response.Redirect then the response will be a 300-series redirection and not a regular 200 page. When the browser receives a redirection the browser will make a subsequent GET request for the redirected-to location.

If the user presses the Stop button after the POST request was made but before the 300 redirection was received then the browser will ignore the server response.

...unless you've got Javascript running on your page, but then it's no different to the page making any other request.

Upvotes: 0

ChrisLively
ChrisLively

Reputation: 88064

The Response.Redirect process is usually:

  1. Page loaded by browser - all javascript type stuff just as showing alert('hi'); is functional.

  2. User clicks a button that causes a postback.

  3. onclick code, in the post back, does a Response.Redirect which sends a message to the browser to load a different page.

  4. browser loads new page (go to 1).

If the user clicks STOP somewhere between 3 and 4, then the new page will not be loaded and the old page is still active. Therefore, any javascript on that page will still be executable, such as alert('hi');


Now, there are certain situations when Ajax and jQuery is involved where it is entirely possible that a page gets into a situation in which previously available javascript no longer functions; but that isn't exactly what you were talking about.

Upvotes: 0

Related Questions