[英]Response.redirect not working
I'm using visual studio 2010 asp.net website. 我正在使用Visual Studio 2010 asp.net网站。 I made an aspx.
我做了一个aspx。 page called forgetpassword.aspx and added a button with the codes
页面,称为忘记密码.aspx,并添加了带有代码的按钮
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. 但是,页面保持不变,该按钮无法重定向到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: 这是我的forgetpassword.aspx页面的源代码视图(html):
<%@ 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>
You might need to specify the path, as the way shown in MasterPageFile="~/Master.master"
. 您可能需要指定路径,如
MasterPageFile="~/Master.master"
。
Response.Redirect("~/company.aspx");
EDIT 编辑
You need to use same ContentPlaceHolder
ID across all the files, by default it is MainContent
. 您需要在所有文件中使用相同的
ContentPlaceHolder
ID,默认情况下为MainContent
。
<asp:ContentPlaceHolder id="MainContent" runat="server">
</asp:ContentPlaceHolder>
And also make sure to place your Button
inside a form
. 还要确保将
Button
放置在form
。
<form action="/" method="post" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>
Now the redirect should work. 现在重定向应该起作用了。
redirect should take a url as a parameter however you are only passing a string "company.aspx". 重定向应该以url作为参数,但是您只传递字符串“ company.aspx”。 Please try this
请尝试这个
Response.Redirect(~/(path)/company.aspx);
Replace path with correct file path in your project 用项目中的正确文件路径替换路径
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. 这将避免需要从服务器端调用事件。
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 https://msdn.microsoft.com/zh-CN/library/a8wa7sdt(v=vs.110).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.