简体   繁体   English

如何从一个ASP.NET页面重定向到另一个

[英]How to redirect from one ASP.NET page to another

如何通过按钮从一个ASP.NET页面重定向到另一个(“Webform2.aspx”)?

您可以使用Response.Redirect()从一个页面重定向到另一个页面

设置按钮的PostBackUrl属性,如下所示:

button1.PostBackUrl= "Webform2.aspx";

You can redirect to another ASP.NET page using the code below : 您可以使用以下代码重定向到另一个ASP.NET页面:

Response.Redirect("Webform.aspx");

This is the simplest way 这是最简单的方法

Well there are lot of ways. 那么有很多方法。 Response.Redirect , Server.Transfer , Javascript call to the page. Response.RedirectServer.Transfer ,Javascript调用页面。

Javascript call is required when u have no server side actions for the button. 如果您没有按钮的服务器端操作,则需要Javascript调用。 onclick="javascript:window.location.href = Webform2.aspx?id='<%=Request.QueryString["id"]%>' " onclick="javascript:window.location.href = Webform2.aspx?id='<%=Request.QueryString["id"]%>'

Server.Transfer will do a re-direct at server side. Server.Transfer将在服务器端进行Server.Transfer ie, The browser will still show after the response from webform2. 即,浏览器仍会在webform2的响应后显示。 Webform1.aspx will re-direct the request to webform2 and webform2 will give the req. Webform1.aspx会将请求重定向到webform2,webform2将提供req。 (Req = 1, Res = 1) (Req = 1,Res = 1)

Response.Redirect : webform1 will send a response asking the browser to make a new request to webform2. Response.Redirect :webform1将发送一个响应,要求浏览器向webform2发出新请求。 In this case, the browser will change the url as it is making a new req to webform2.(Req = 1 + 1, Res = 1+1) 在这种情况下,浏览器将更改URL,因为它正在向webform2发出新的请求。(Req = 1 + 1,Res = 1 + 1)

There is one more way, form.submit() if you are interested. 如果你感兴趣,还有一种方法form.submit() The traditional html form submit. 传统的html表单提交。

Forgot to mention the best of all, the cross-page postback with PostBack url.. http://aspdotnetcode.source-of-humor.com/TipsAndTricks/General/CrossPagePostbackAspNetCrossPagePostback.aspx 忘了提到最好的,用PostBack url进行跨页回发.. http://pdpdotnetcode.source-of-humor.com/TipsAndTricks/General/CrossPagePostbackAspNetCrossPagePostback.aspx

Personally, if all you're wanting to do is load a new page when a button is clicked, I would do this with client-side script. 就个人而言,如果您想要做的就是在单击按钮时加载新页面,我会使用客户端脚本执行此操作。

You could use a JS library for this (eg: jQuery), like so: 您可以使用JS库(例如:jQuery),如下所示:

jQuery jQuery的

$(function() {
  $('#<%= button1.ClientID %>').click(function() { 
      window.location.href = "Webform2.aspx"; 
      });
});

ASP.NET ASP.NET

<asp:Button id="button1" runat="server"/>

Or, for a specifically ASP.NETesque way to do it, you can use Button.PostBackUrl as Antonio suggests, which still uses client-side script but means you don't have to write it yourself. 或者,对于一个特定的ASP.NETesque方法,您可以使用Button.PostBackUrl如Antonio建议的那样,它仍然使用客户端脚本,但这意味着您不必自己编写它。 The HTML for the button renders as: 该按钮的HTML呈现为:

<input type="submit" name="button1" value="Button" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;button1&quot;, &quot;&quot;, true, &quot;&quot;, &quot;webform2.aspx&quot;, false, false))" id="button1" />

If you've got other processing to do server-side and you need to redirect afterwards, use Response.Redirect("Webform2.aspx"); 如果你有其他处理要做服务器端,然后需要重定向,请使用Response.Redirect("Webform2.aspx"); in your click handler. 在你的点击处理程序中

If that's not working for you, please add some more detail to your question to explain what's happening. 如果这不适合您,请在您的问题中添加更多细节,以解释发生了什么。

You can use below code : 您可以使用以下代码:

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

Notice that default2.aspx is your second web page name and you 请注意, default2.aspx是您的第二个网页名称

Response.Redirect(string url) issues a 302 HTTP status code instructing the client to redirect to url . Response.Redirect(string url)发出302 HTTP状态代码,指示客户端重定向到url The browser will issue a new request for url and the URL will change in the address bar. 浏览器将发出新的url请求,URL将在地址栏中更改。

Server.Transfer(string path) terminates execution of the current page and starts execution of a new page on the specified path ie internally within IIS. Server.Transfer(string path)终止当前页面的执行,并开始在指定path上执行新页面,即在IIS 内部执行 Therefore the URL in the browser address bar will not be changed. 因此,浏览器地址栏中的URL不会更改。 The page you transfer to must be an aspx page in the same web site. 您转移到的页面必须是同一网站中的aspx页面。

The differences are subtle but important. 差异很微妙但很重要。 A simple way to think about this is to ask yourself "should the user bookmark/favorite this URL?". 考虑这个的一个简单方法是问自己“用户应该书签/喜欢这个URL吗?”。 Use Response.Redirect if the URL has changed and future visits to the content should be on the new URL. 如果URL已更改,则使用Response.Redirect ,并且以后对内容的访问应该在新URL上。 Use Server.Transfer if the URL is correct and current but you need to display different content this one time - maybe you are displaying an error message or you need the user to enter their credentials to continue or there is some other reason why the content should change but the URL should not. 使用Server.Transfer如果URL正确且最新,但您需要一次显示不同的内容 - 可能您正在显示错误消息,或者您需要用户输入其凭据才能继续,或者还有其他原因要求更改但URL不应该。

Either of the above can be used within the Click event handler of an ASP.NET Button control in your code-behind: 上面的任何一个都可以在代码隐藏中的ASP.NET Button控件的Click事件处理程序中使用:

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("Webform2.aspx");
      // OR
    Server.Transfer("Webform2.aspx");
}

Both Response.Redirect and Server.Transfer methods are used to transfer a user from one web page to another web page. Response.Redirect和Server.Transfer方法都用于将用户从一个网页传输到另一个网页。 Both methods are used for the same purpose but still there are some differences as follows. 两种方法都用于相同的目的,但仍然存在如下差异。

The Response.Redirect method redirects a request to a new URL and specifies the new URL while the Server.Transfer method for the current request, terminates execution of the current page and starts execution of a new page using the specified URL path of the page. Response.Redirect方法将请求重定向到新URL,并在当前请求的Server.Transfer方法中指定新URL,终止当前页面的执行,并使用指定的页面URL路径开始执行新页面。

Both Response.Redirect and Server.Transfer has same syntax like: Response.Redirect和Server.Transfer都具有相同的语法:

Response.Redirect("UserDetail.aspx"); 的Response.Redirect( “UserDetail.aspx”); Server.Transfer("UserDetail.aspx"); Server.Transfer的( “UserDetail.aspx”);

Before touching on more points I want to explain some HTTP status codes, these are important for the understanding of the basic differences between these two. 在讨论更多要点之前,我想解释一些HTTP状态代码,这些对于理解这两者之间的基本差异非常重要。 The HTTP status codes are the codes that the Web server uses to communicate with the Web browser or user agent. HTTP状态代码是Web服务器用于与Web浏览器或用户代理通信的代码。 Response.Redirect sends an HTTP request to the browser, then the browser sends that request to the web server, then the web server delivers a response to the web browser. Response.Redirect向浏览器发送HTTP请求,然后浏览器将该请求发送到Web服务器,然后Web服务器向Web浏览器发送响应。 For example, suppose you are on the web page "UserRegister.aspx" page and it has a button that redirects you to the "UserDetail.aspx" web page. 例如,假设您在网页“UserRegister.aspx”页面上,它有一个按钮,可以将您重定向到“UserDetail.aspx”网页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM