Redirecting request from one page to another page in asp.net

Hello dear experts
I have two aspx pages (page 1 and 2), I want to send the request to page two from page one, I mean that I want to avoid access to page 2 when a user directly going to page 2 and the user should go to page 2 from page 1 and should be redirected to page 1 when requesting directly to see the page 2.
Please guide me how I could achieve this task.
Thanks


alirezakavehAsked:
Who is Participating?
 
Richard LeeConnect With a Mentor Software EnthusiastCommented:
Try the following
// Check if page was directly accessed
if(Request.UrlReferrer == null || String.IsNullOrEmpty(Request.UrlReferrer.ToString()))
{
    Response.Redirect("Page1.aspx");
}
 
// Check if the page had a referrer but not the correct one
if(Request.UrlReferrer == null || !Request.UrlReferrer.GetLeftPart(UriPartial.Path).ToLower().Contains("page1.aspx"))
{
      Response.Redirect("Page1.aspx");
}

Open in new window

0
 
BlomholmCommented:
This should work
//In page 1 this would transfer to page 2:
Server.Transfer("./submitreport.aspx");
 
//In page 2 in page_load:
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack && PreviousPage != null)
    {
         //do stuff
    }
    else {
        Response.write("not allowed to access directly")
    }

Open in new window

0
 
Richard LeeSoftware EnthusiastCommented:
In your page load event in page 2 do the following:

// Check if page was directly accessed
if(String.IsNullOrEmpty(Request.UrlReferrer.ToString()))
{
    Response.Redirect("Page1.aspx");
}

// Check if the page had a referrer but not the correct one
if(!Request.UrlReferrer.GetLeftPart(UriPartial.Path).ToLower().Contains("page1.aspx"))
{
      Response.Redirect("Page1.aspx");
}
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Richard LeeSoftware EnthusiastCommented:
The above could be re-written with the PreviousPage property as follows:
// Check if page was directly accessed
if(String.IsNullOrEmpty(PreviousPage.Request.Url.ToString()))
{
    Response.Redirect("Page1.aspx");
}
 
// Check if the page had a referrer but not the correct one
if (!PreviousPage.Request.Url.GetLeftPart(UriPartial.Path).ToLower().Contains("page1.aspx"))
{
	Response.Redirect("Page1.aspx");
}

Open in new window

0
 
sunithnairCommented:
Better to use Request.UrlReferrer as DaTribe mentioned in the 2nd post here. PreviousPage will be null if you do a response.redirect, it will have value only if it is a server.transfer
0
 
nmarunCommented:
When it comes to Response.Redirect, always use the overloaded method:

Response.Redirect("NewPage.aspx", false);

http://support.microsoft.com/kb/312629
0
 
alirezakavehAuthor Commented:
I am getting NullReferenceException error and if i use try catch nothing will happen
0
 
alirezakavehAuthor Commented:
Thanks it worked
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.