Solved

ASP.NET C# Response.Redirect

Posted on 2011-02-14
7
1,152 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
abstract class C# 1 35
Converting PHP triple des decrypt to c# 9 62
C# DataTable (in memory) vs SQL Lookups 4 28
C# LINQ 5 18
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

679 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