C# 在asp .net中返回上一页的超链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14037870/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:29:38  来源:igfitidea点击:

Hyperlink to go back to previous page in asp .net

c#asp.net.netasp.net-mvcasp.net-mvc-3

提问by user1400915

I have a page in asp .net (http://localhost/error/pagenotfound).

我在asp .net中有一个页面 (http://localhost/error/pagenotfound).

There is a link in page, on clicking on which has to go back to previous page from where I came from.

页面中有一个链接,点击它必须返回到我来自的上一页。

<a href="#">Go Back to Previous Page.</a> 

How can I go back to previous page by taking from history

如何通过历史记录返回上一页

采纳答案by kleinohad

you can use this:

你可以使用这个:

<a href='javascript:history.go(-1)'>Go Back to Previous Page</a>

回答by Manish Nagar

use this code

使用此代码

    <html>
    <head>
    <script>
    function goBack()
      {
      window.history.back()
      }
    </script>
    </head>
    <body>

    <a href="#" onclick="goBack()">Back</a>

    </body>
    </html>

回答by ????

If you are using asp.net then remember that

如果您使用的是 asp.net,请记住

javascript:history.go(-1)

and

window.history.back()

Both will take you to back page.
But the previous page will not be exactly previous page.

两者都会带你回到后页。
但上一页将不完全是上一页。

For example

例如

Suppose you are on page Default.aspxand there is a asp:button
Now when you click on the button and you are back on Default.aspx
in this situation your previous page is still you Default.aspx

假设您在页面上Default.aspx并且asp:button
当您单击按钮时有一个Now 并且您在这种情况下返回Default.aspx
上一页仍然是您的 Default.aspx

Take another exapmle
You have two pages Default1.aspxand Default2.aspx
Condition 1:- button clicked on Default1.aspxwhich redirect you to Default2.aspx
ok your previous page is Default1.aspx
Condition 2:- button clicked on Default1.aspxand post back on the same Default1.aspxpage
Now your previous page is still Default1.aspx

再举一个例子
你有两个页面Default1.aspxDefault2.aspx
条件 1:-点击按钮Default1.aspx,你重定向到Default2.aspx
前一页是Default1.aspx
条件 2:-按钮点击Default1.aspx并在同一Default1.aspx页面 上发回
现在你的前一页仍然Default1.aspx



Edit

编辑

 protected void Page_Load(object sender, EventArgs e)
 {
     if( !IsPostBack )
     {
        ViewState["RefUrl"] = Request.UrlReferrer.ToString();
     }
  }

and use this in back button as follows

并在后退按钮中使用它,如下所示

 protected void Button3_Click(object sender, EventArgs e)
 {
      object refUrl = ViewState["RefUrl"];
      if (refUrl != null)
          Response.Redirect((string)refUrl);
 }

回答by Stack User

You should add an attribute which is about onclick like below:

您应该添加一个关于 onclick 的属性,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
    yourButton.Attributes.Add("onClick", "javascript:history.back(); return false;");
}

protected void yourButtonClick(object sender, EventArgs e)
{
      Response.Write("Stackoverflow <br/>");
}

This is about using a button to go previous page, you can modify it according to your changes.

这是关于使用按钮转到上一页,您可以根据您的更改进行修改。

回答by Rizwan Gill

For Going to previous page

前往上一页

First Method

第一种方法

<a href="javascript: history.go(-1)">Go Back</a>

Second Method

第二种方法

<a href="##" onClick="history.go(-1); return false;">Go back</a> 

if we want to more than one step back then increase

如果我们想后退不止一步,那么增加

For going 2 steps back history.go(-2)
For going 3 steps back history.go(-3)
For going 4 steps back history.go(-4)
and so on........

回答by alphan

I found this example

我找到了这个例子

<input type="button" value=" &lt;-- BACK " onclick="history.go(-1);return false;">

Just put this your page, it is working.

把这个放在你的页面上,它正在工作。

回答by Mark Ludwinski

I've been looking at this all weekend and haven't found the answer I was looking for. The problem is, after a postback history.go(-1); will not go back to where you want to go - that is, the page that got you there (with any changes made).

我整个周末都在看这个,但没有找到我想要的答案。问题是,在回发之后 history.go(-1); 不会回到你想去的地方——也就是让你到达那里的页面(做了任何更改)。

Using Request.UrlReferrer.ToString(); will reload the page you are going back to and you may be like me and not want to lose any changes that were made to the page. Above Rizwan Gill in 2013 hit on the answer that you want to go back 2, 3 or 4 pages but he didn't give a good way to do it.

使用 Request.UrlReferrer.ToString(); 将重新加载您要返回的页面,您可能和我一样,不想丢失对页面所做的任何更改。在 2013 年的 Rizwan Gill 上面找到了你想要返回 2、3 或 4 页的答案,但他没有给出一个很好的方法来做到这一点。

The best way to do this is actually to use a pop-up modal for your page when it is called if your are writing the calling page but if you want to do it this way and have a back button you'll have to remember each postback and count them.

执行此操作的最佳方法实际上是在调用页面时为您的页面使用弹出模式,如果您正在编写调用页面,但如果您想以这种方式执行此操作并有一个后退按钮,则必须记住每个回发并计算它们。

So, start with your hidden field (in your .aspx file) :

因此,从您的隐藏字段(在您的 .aspx 文件中)开始:

    <asp:hiddenfield id="fldPostbackCount"
        value="0" 
        runat="server"/>

Then increment the value with each postback (in your .aspx.vb file):

然后在每次回发时增加该值(在您的 .aspx.vb 文件中):

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        fldPostbackCount.Value = 1
    Else
        fldPostbackCount.Value = Int32.Parse(fldPostbackCount.Value) + 1
    End If

End Sub  '  Page_Load() 

(I'm sure the .cs people can handle the to C conversion on this)

(我确信 .cs 人可以处理到 C 的转换)

Now back to your .aspx file you have to have a history button:

现在回到您的 .aspx 文件,您必须有一个历史记录按钮:

    <a href="javascript:history.go(-<%: fldPostbackCount.Value %>)" title="Back to Previous Page">Back to Previous Page</a>

回答by Brian Santacruz

Following the example of ????

按照 ???? 的例子

I would suggest to do the following and avoid any cycle on the url while posting back.

我建议执行以下操作,并在回发时避免在 url 上出现任何循环。

string from = Request.UrlReferrer.ToString();
string here = Request.Url.AbsoluteUri.ToString();

if(from != here)
    Session["page"] = Request.UrlReferrer.ToString();

On your button click

在您的按钮上单击

object refUrl = Session["page"];
if (refUrl != null)
    Response.Redirect((string)refUrl);

This will save always the PAGE you are coming from.

这将始终保存您来自的页面。

Hope it helps.

希望能帮助到你。

回答by Hosein Yusefpour

In your C# code just write this:

在您的 C# 代码中,只需编写以下代码:

Response.Write("<script>history.go(-1);</script>");