ASP.NET 和 C# 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15983545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET and C# Redirect
提问by Skrubb
I am working on a project for school, and this is an extra credit part. I have a project started in VS 2010 using master pages, and what I'm trying to do is get a "Submit" button to redirect people to the "MyAccounts.aspx" page. My current code for the ASP part for the button looks like this:
我正在为学校做一个项目,这是一个额外的学分部分。我有一个在 VS 2010 中使用母版页启动的项目,我想要做的是获得一个“提交”按钮,将人们重定向到“MyAccounts.aspx”页面。我当前的按钮 ASP 部分代码如下所示:
<asp:Button ID="btnTransfer" runat="server" Text="Submit"/>
<asp:Button ID="btnTransfer" runat="server" Text="Submit"/>
I have tried adding in the OnClick
option, as well as the OnClientClick
option. I have also added this code to the Site.Master.cs file as well as the Transfer.aspx.cs file:
我已经尝试添加OnClick
选项,以及OnClientClick
选项。我还将此代码添加到 Site.Master.cs 文件以及 Transfer.aspx.cs 文件中:
protected void btnTransfer_Click(object sender, EventArgs e)
{
Response.Redirect(Page.ResolveClientUrl("/MyAccounts.aspx"));
}
When I run this and view the project in my browser, the whole thing runs fine, but when I click on the "Submit" button, it just refreshes the current page and does not properly redirect to the MyAccounts page. Anyone have any ideas for me?
当我运行它并在我的浏览器中查看项目时,整个过程运行良好,但是当我单击“提交”按钮时,它只会刷新当前页面并且没有正确重定向到 MyAccounts 页面。有人对我有什么想法吗?
采纳答案by MikeSmithDev
You are doing it almost correctly, you just haven't put the correct pieces together. On Transfer.aspx, your button should be:
你几乎做对了,只是你没有把正确的部分放在一起。在 Transfer.aspx 上,您的按钮应该是:
<asp:Button ID="btnTransfer" OnClick="btnTransfer_Click" runat="server" Text="Submit"/>
and your code behind should be like what @KendrickLamar said:
并且您后面的代码应该像@KendrickLamar 所说的那样:
protected void btnTransfer_Click(object sender, EventArgs e)
{
Response.Redirect("~/MyAccounts.aspx");
}
The OnClick
event tells it what to execute on post-back when the users clicks the button. This is in the code-behind for Transfer.aspx, not the site master.
OnClick
当用户单击按钮时,该事件告诉它在回发时执行什么。这是 Transfer.aspx 的代码隐藏,而不是站点管理员。