Every ASP.NET developer, at some point or another, has created a page with a Button that, when clicked, redirects the user to some other page. This is typically implemented by adding a Button control to the page, creating a Click event handler, and adding a Response.Redirect, perhaps passing along some user input through the querystring. There are two shortcomings with this approach:
- It involves an extra round trip to the server for the Button postback, which just generates a redirect message.
- If there is an associated TextBox where the user enters some value and that is transmitted via the querystring, you need to worry about what happens if the user presses the Enter key while focused in this TextBox. There's a postback, most likely, but what Button is considered clicked? This issue becomes more apparent if you have several TextBoxes on a page, each with a Button, that, when clicked, takes the user to some other page passing along the related TextBox's value through the querystring. See Enter and the Button Click Event for a thorough discussion on this topic...
The good news is that a jot of JavaScript can help surmount both of these shortcomings. I've created a simple custom server control that packages this JavaScript and behavior into a single, easy to use control, which I call RedirectButton. You can learn more about this control, as well as download the complete source code, at RedirectButton - Redirect Users With the Click of a Buton.
Happy Programming!
Check out more of my free ASP.NET server controls at My Code Projects page!