Sarahdonner
Sarahdonner

Reputation: 1

Response.redirect not working

I'm using visual studio 2010 asp.net website. I made an aspx. page called forgetpassword.aspx and added a button with the codes

 protected void Button1_Click(object sender, EventArgs e)
  {
    Response.Redirect("company.aspx");
  }

I even tried:

 protected void Button1_Click(object sender, EventArgs e)
  {
    Response.Redirect("company.aspx", true);
  }

However, the page remains the same, the button is unable to redirect to company.aspx. I suspect there could be something wrong with my master page, but I am not sure. Please advise!

Here is my master page:

   <%@ Master Language="C#" AutoEventWireup="true"        CodeFile="Master.master.cs" Inherits="Master" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   <html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
   <title>  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="description" content="">
  <meta name="author" content="">
   <title>Home | Triangle</title>
  <link href="css/bootstrap.min.css" rel="stylesheet">
  <link href="css/font-awesome.min.css" rel="stylesheet">
  <link href="css/animate.min.css" rel="stylesheet"> 
  <link href="css/lightbox.css" rel="stylesheet"> 
  <link href="css/main.css" rel="stylesheet">
  <link href="css/responsive.css" rel="stylesheet">

  <!--[if lt IE 9]>
    <script src="js/html5shiv.js"></script>
    <script src="js/respond.min.js"></script>
  <![endif]-->       
  <link rel="shortcut icon" href="images/ico/favicon.ico">
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="images/ico/apple-touch-icon-144-precomposed.png">
    <link rel="apple-touch-icon-precomposed" sizes="114x114" href="images/ico/apple-touch-icon-114-precomposed.png">
  <link rel="apple-touch-icon-precomposed" sizes="72x72" href="images/ico/apple-touch-icon-72-precomposed.png">
  <link rel="apple-touch-icon-precomposed" href="images/ico/apple-touch-icon-57-precomposed.png"></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
 </head>
 <body></body>
  </html>

Here is the source view (html) of my forgetpassword.aspx page:

   <%@ Page Title="" Language="C#" MasterPageFile="~/Master.master" AutoEventWireup="true" CodeFile="forgetpassword.aspx.cs" Inherits="_Default" %>

  <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  </asp:Content>
  <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1"   Runat="Server">
  <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
  </asp:Content>

Upvotes: 0

Views: 10576

Answers (4)

abdul
abdul

Reputation: 1592

You might need to specify the path, as the way shown in MasterPageFile="~/Master.master".

Response.Redirect("~/company.aspx");

EDIT

You need to use same ContentPlaceHolder ID across all the files, by default it is MainContent.

<asp:ContentPlaceHolder id="MainContent" runat="server">
</asp:ContentPlaceHolder>

And also make sure to place your Button inside a form.

<form action="/" method="post" runat="server">
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>

Now the redirect should work.

Upvotes: 1

Damitha Shyamantha
Damitha Shyamantha

Reputation: 534

Try this

       protected void Button1_Click(object sender, EventArgs e)
      {
         if (Response.IsClientConnected)
         {
            // If still connected, redirect
            // to another page. 
            Response.Redirect("company.aspx", false);
         }
      }

https://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.110).aspx

Upvotes: 0

Imad
Imad

Reputation: 7490

If it is just a redirect use

PostBackUrl="company.aspx"

like

<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="company.aspx"/>

This will avoid need of calling event from server side.

Upvotes: 0

Junior John
Junior John

Reputation: 145

redirect should take a url as a parameter however you are only passing a string "company.aspx". Please try this

Response.Redirect(~/(path)/company.aspx); 

Replace path with correct file path in your project

Upvotes: 0

Related Questions