Developer
Developer

Reputation: 477

Redirecting back from where user came from in ASP.NET User Control

I have a user control which overriding Page_load method, however what I want is, redirect user from where they came from, it can be on "ON INIT method" or any method as long as user is redirected.

This is what I got at the moment,

public partial class ASPUserControl: System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Upvotes: 1

Views: 2945

Answers (2)

Tim Schmelter
Tim Schmelter

Reputation: 460158

You can use the UrlReferrer

Uri previousUri = Request.UrlReferrer;
if(previousUri != null)
    Response.Redirect(previousUri.ToString());

However, the UrlReferrer is only populated if the user clicked on a link(f.e. via PostBackUrl) or similar, not if you've used Response.Redirect to go to this page. If you've used Server.Transfer you could use Page.PreviousPage to get the url.

Upvotes: 2

Onur Topal
Onur Topal

Reputation: 3061

as long as he is requesting the page you can redirect user when/where ever you want onInit Page_Load or even a button click event. It is better if you can do it as early as possible to avoid unnecessary process.

Upvotes: 0

Related Questions