开发者

How to redirect from one ASP.NET page to another

开发者 https://www.devze.com 2023-03-04 16:01 出处:网络
How do I re开发者_Go百科direct from one ASP.NET page to another (\"Webform2.aspx\") by means of a button?You can redirect from one page to another using Response.Redirect()set PostBackUrl property of

How do I re开发者_Go百科direct from one ASP.NET page to another ("Webform2.aspx") by means of a button?


You can redirect from one page to another using Response.Redirect()


set PostBackUrl property of button, like this :

button1.PostBackUrl= "Webform2.aspx";


You can redirect to another ASP.NET page using the code below :

Response.Redirect("Webform.aspx");

This is the simplest way


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:

jQuery

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

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. The HTML for the button renders as:

<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"); in your click handler.

If that's not working for you, please add some more detail to your question to explain what's happening.


Well there are lot of ways. Response.Redirect, Server.Transfer, Javascript call to the page.

Javascript call is required when u have no server side actions for the button. onclick="javascript:window.location.href = Webform2.aspx?id='<%=Request.QueryString["id"]%>'"

Server.Transfer will do a re-direct at server side. i.e, The browser will still show after the response from webform2. Webform1.aspx will re-direct the request to webform2 and webform2 will give the req. (Req = 1, Res = 1)

Response.Redirect: webform1 will send a response asking the browser to make a new request to 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)

There is one more way, form.submit() if you are interested. The traditional html form submit.

Forgot to mention the best of all, the cross-page postback with PostBack url.. http://aspdotnetcode.source-of-humor.com/TipsAndTricks/General/CrossPagePostbackAspNetCrossPagePostback.aspx


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


Response.Redirect(string url) issues a 302 HTTP status code instructing the client to redirect to url. The browser will issue a new request for url and the URL will change in the address bar.

Server.Transfer(string path) terminates execution of the current page and starts execution of a new page on the specified path i.e. internally within IIS. Therefore the URL in the browser address bar will not be changed. The page you transfer to must be an aspx page in the same web site.

The differences are subtle but important. A simple way to think about this is to ask yourself "should the user bookmark/favorite this URL?". Use Response.Redirect if the URL has changed and future visits to the content should be on the new 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.

Either of the above can be used within the Click event handler of an ASP.NET Button control in your code-behind:

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. 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.

Both Response.Redirect and Server.Transfer has same syntax like:

Response.Redirect("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. The HTTP status codes are the codes that the Web server uses to communicate with the Web browser or user agent. 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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号