0

I have tried both Response.Redirect and Server.Transfer to take me to a new page on button click, but every time my page just refreshes and I am never redirected to the new page. I have verified the page exists in my project and even copied/pasted the name of the page into my syntax to make sure no weird spaces or anything, but I NEVER get redirected

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        filldropdowns();
    }
}

protected void btn1_OnClick(object sender, EventArgs e)
{
    Response.Redirect("page2.aspx");
    Server.Transfer("page2.aspx"); 
}

    <div id="btn11" algn="center">
        <asp:Button ID="ClickBtn1" runat="server" Text="Push Me" OnClick="btn1_OnClick" OnClientClick="return ValidateData();" />
    </div>

<script type="text/javascript">
    function ValidateData() {
        var name;
        name = document.getElementById("txtName").value;
        if (name == '' || name.trim() == '') {
            alert("Please enter a valid name");
            return false;
        }
    }
</script>

EDIT
using the ~/ will allow a redirect but it throws the below error. The page does exist!

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /page2.aspx

EDIT 2
This is the markup for my page2 if that matters

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="page2.aspx.cs" Inherits="page2" %>

And if I use localhost in my redirect it loads no problem, but I am ready to push this out into the interweavings of the web and can not use localhost anymore. What should I change this to?

http://localhost:1444/TestProject/page2.aspx

EDIT 3
If it helps (or matters) the full location to the .aspx page that I want to redirect to is this:

C:\Users\Habib\Documents\Visual Studio 2015\Projects\Test\TestProject\Page2.aspx

1 Answers1

0

You have shown syntax that is known to work whether it be means of Response.Redirect() or Server.Transfer() those methods are tried and true to be successful. If when you step through your code it hits your break point for the re-direct, then show us what you have in your Page_Load() for page2.aspx.

And we can help further diagnose and/or troubleshoot from that.

Bob Goblin
  • 1,251
  • 3
  • 16
  • 33