Solved

ASP.NET C# Response.Redirect

Posted on 2011-02-14
7
1,163 Views
Last Modified: 2012-08-14
I don't know why Response.Redirect does not work if I put it in a class?

public static void Check_Variable()
        {
            if (HttpContext.Current.Session["selected_year"] == null)
            {
                Response.Redirect("Option.aspx");
            }

            selected_risk_rating_year = int.Parse(Session["selected_risk_rating_year"].ToString());
        }

any one can help?
0
Comment
Question by:mawingho
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 12

Accepted Solution

by:
Praveen Kumar earned 300 total points
ID: 34894464
System.Web.HttpContext.Current.Response.Redirect(" Option.aspx");

From a good design perspective, you should let the class library simply return a value that you use to determine where to redirect to in the presentation layer (the .aspx pages)
0
 

Author Comment

by:mawingho
ID: 34894515
I tried to do the following

but the sPath always return false....why?

public static void CheckVariable()
        {
            if (HttpContext.Current.Session["selected_year"] == null)
            {
                string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;

                if (sPath == "/Option.aspx")
                {
                    HttpContext.Current.Response.Redirect("Option.aspx");
                }
            }
           
            selected_year = int.Parse(HttpContext.Current.Session["selected_year"].ToString());
        }
0
 
LVL 12

Assisted Solution

by:Praveen Kumar
Praveen Kumar earned 300 total points
ID: 34894544
What you want be in sPath?

public static void CheckVariable()
        {
            if (HttpContext.Current.Session["selected_year"] == null)
            {
                string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
sPath = sPath .Substring(sPath .LastIndexOf("/")+1);
                if (sPath == "/Option.aspx")
                {
                    HttpContext.Current.Response.Redirect("Option.aspx");
                }
            }
           
            selected_year = int.Parse(HttpContext.Current.Session["selected_year"].ToString());
        }
String url = Request.Url.AbsolutePath;

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 12

Assisted Solution

by:Praveen Kumar
Praveen Kumar earned 300 total points
ID: 34894547
Sorry, try:

public static void CheckVariable()
        {
            if (HttpContext.Current.Session["selected_year"] == null)
            {
                string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
                sPath = sPath .Substring(sPath .LastIndexOf("/")+1);  

                if (sPath == "Option.aspx")
                {
                    HttpContext.Current.Response.Redirect("Option.aspx");
                }
            }
           
            selected_year = int.Parse(HttpContext.Current.Session["selected_year"].ToString());
        }
0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 100 total points
ID: 34894564
you need to reference context.

using

System;
using

System.Data;
using

System.Configuration;
using

System.Web;
using

System.Web.Security;
using

System.Web.UI;
using

System.Web.UI.WebControls;
using

System.Web.UI.WebControls.WebParts;
using

System.Web.UI.HtmlControls;
///

<summary>
///

Summary description for TestBasePage
///

</summary>
public

class TestBasePage : System.Web.UI.Page
{


protected override void OnInit(EventArgs e)
{


this.Load += new EventHandler(TestBasePage_Load);

base.OnInit(e);
}


void TestBasePage_Load(object sender, EventArgs e)
{


HttpContext.Current.Session["test"] = "test";
Response.Write((

string)HttpContext.Current.Session["test"]);
Context.Response.Redirect(

"");
}

}
0
 
LVL 11

Assisted Solution

by:SAMIR BHOGAYTA
SAMIR BHOGAYTA earned 100 total points
ID: 34894744
Hi, you don't have to put this function like this. It is wrong method to use it.
0
 

Author Comment

by:mawingho
ID: 34894814
because I put it on the site.master
I however, the Option page also have the site.master
right?
So I have to stop the redirect if it is on the Option page.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question