Solved

ASP.NET C# Response.Redirect

Posted on 2011-02-14
7
1,138 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
7 Comments
 
LVL 12

Accepted Solution

by:
Mamtha1982 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:Mamtha1982
Mamtha1982 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 12

Assisted Solution

by:Mamtha1982
Mamtha1982 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now