Mac
Mac

Reputation: 7543

How to know that we come to a page after redirecting from another page

I'm using a single page for two different purposes. By default it has one behavior and on coming to same page after redirecting via a link button it has a different behavior, like changing master page, etc...

How can I detect this and change the behavior accordingly?

Upvotes: 0

Views: 188

Answers (5)

David Espart
David Espart

Reputation: 11780

You can know the page where you come from with the referer field that comes in the header. In asp.net you can retrieve it like this:

string MyReferrer;

if(Request.UrReferrer != null)
{
    MyReferrer = Request.UrlReferrer.ToString();
}

Upvotes: 1

Chris
Chris

Reputation: 27619

If you have one page with two different behaviours then I would suggest that you want something like a querystring parameter to differentiate between the two purposes (eg somepage.aspx?mode=changeMaster). You can then check for this value and change your behaviour accordingly.

If you are only every doing the second behaviour from one place then its probably easiest to let it have a default behaviour rather than requiring the mode parameter (so you wouldn't have to change all your links to the page, just that one linkbutton). This should be much more reliable than relying on referrers and other such things which aren't always sent.

Upvotes: 1

Timothy S. Van Haren
Timothy S. Van Haren

Reputation: 8966

Assuming you have control over the pages that a user redirects to and from, set a Session variable when you perform an action that your page should base its behavior upon.

For instance, in a LinkButton_Click event, you could set a Session variable like so:

protected void LinkButton_Click(object sender, EventArgs e)
{
    Session["Source"] = "MyLinkButton";
}

And in your page's Page_Load or Page_Init event, check the value of that Session variable and perform the page's change in behavior based on the value in that Session variable.

protected void Page_Init(object sender, EventArgs e)
{
    if (Session["Source"] == "MyLinkButton")
    {
        // do something
    }
    else if (Session["Source"] == "SomethingElse")
    {
        // dome something else
    }
}

Upvotes: 0

Piotr Rochala
Piotr Rochala

Reputation: 7781

Use HttpRequest.UrlReferrer Property

Upvotes: 1

Marcus Johansson
Marcus Johansson

Reputation: 2667

I don't know asp.net but I'm pretty shure you can get it from the HTTP-headers referer/referrer field.

http://en.wikipedia.org/wiki/HTTP_referrer

Upvotes: 0

Related Questions