p4bl0
p4bl0

Reputation: 5653

How can i have Response.Redirect() work from MasterPage?

I have a problem: when i call a Response.Redirect() from the MasterPage it doesn't work. Well, debugging i can see that until the Pre_Render() method the target page is loaded, but then is rendered the previous page.

Here's some code to better explain:

(from MasterPageMain.master.cs)

protected void Page_Init(object sender, EventArgs e)
{
    string m_QueryStringValue = Request.QueryString.Get("action");
    if ((!string.IsNullOrEmpty(m_QueryStringValue)) && (m_QueryStringValue.ToLower() == "send"))
    {
        if (Session["to"] != null && Session["to"] is List<string>) this.SendPageByMail();
        else
        {
            Session.Add("AddressToSend", Request.RawUrl);
            Response.Redirect("~/chooseRecipients.aspx");
        }
    }
}

I have a javascript that adds the querystring adding "action=send" when i click on the Send button.

If i am on page "~/somethingInterestingToSend()" -for example- i want to get on the recipient selection page, but when i click the Send button i see always the same page.

What coul be the mistake?

Upvotes: 6

Views: 18522

Answers (4)

MasterPitch
MasterPitch

Reputation: 21

If you want to redirect not logged in users to a login-page you should check the Request.RawUrl() like this:

string strURL=Request.RawUrl.ToUpper();

if (!strURL.Contains("LOGIN.ASPX") && !strURL.Contains("LOGOUT.ASPX")
    && !strURL.Contains("ERROR.ASPX") && !strURL.Contains("UNDERCONSTRUCTION.ASPX"))
{
    Response.Redirect("~/Login.aspx", false);
}

All other sites will be redirected.

Upvotes: 2

JerKimball
JerKimball

Reputation: 16934

Can't test this theory (running from memory at the moment), but give this a shot:

(sorry, cleaned up the code a bit as well)

protected void Page_Init(object sender, EventArgs e)
{
    string m_QueryStringValue = Request.QueryString.Get("action") ?? "";
    if (m_QueryStringValue.ToLower() == "send")
    {
        if ( (Session["to"] as List<string>) != null) 
        {
            this.SendPageByMail();
        }
        else
        {
            Session.Add("AddressToSend", Request.RawUrl);
            Response.Redirect("~/chooseRecipients.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest()
        }
    }
}

Upvotes: 1

HectorMac
HectorMac

Reputation: 6143

I am not sure I fully understand your description of the problem, but here are a few things to consider:

You mention a send button. If this is an , clicking it fires a javascript postback to the server. This postback is to the original URL. I'm not sure what you are modifying with Javascript, but I don't think it would change the postback URL (and querystring).

If you need to perform logic to redirect you might want to execute in the button click event on the server.

If you don't need to execute any logic on the server, you could to the redirect with javascript:

window.location = "chooseRecipients.aspx";

Upvotes: 1

Echostorm
Echostorm

Reputation: 9814

I don't know if its the root of your problem but I'd change 2 things. I'd change your code to:

Response.Redirect("~/chooseRecipients.aspx", false);

and move the logic to PageLoad

Upvotes: 0

Related Questions