views:

37

answers:

5

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?

A: 

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

Marcus Johansson
@Marcus, that won't work if it's coming from the same page, unless there's only one link in that page that "self redirects" or there's no post-backs to muddy the waters. Checking the referrer and having two separate pages would be the simplest solution though!
Rob
+1  A: 

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();
}
despart
It will throw an exception if Request.UrlReferrer is null.
rochal
Thanks, I edited it to check it out before retrieving the referrer.
despart
+1  A: 

Use HttpRequest.UrlReferrer Property

rochal
A: 

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
    }
}
Tim S. Van Haren
+1  A: 

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.

Chris