Solved

ASP.NET C# Response.Redirect

Posted on 2011-02-14
7
1,140 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

920 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

16 Experts available now in Live!

Get 1:1 Help Now