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, 开发者_如何学Goetc...
How can I detect this and change the behavior accordingly?
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.
Use HttpRequest.UrlReferrer Property
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();
}
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
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
}
}
精彩评论