Asp. NET returns the implementation code of the previous page

Source: Internet
Author: User
Tags object copy interface net return string tostring client
Workaround:

Procedures related to the introduction

Main interface: not shipped order list (http://localhost:18888/Order/UnfilledOrdersList.aspx)

Sub-Interface: Order Details (http://localhost:18888/Order/ViewOrderDetail.aspx?OrderId= ID, where the ID value is the ID of the order information that is selected in the main interface)

The main page is the primary information for an order, and there is a hyperlink control in the GridView that lets you jump to the Order Details interface to view the order details.

The sub interface has a "return" button, which jumps back to the main interface.

The program originally in the Return button is:
Copy CodeThe code is as follows:
#region return button
protected void Btnreturn_click (object sender, EventArgs e)
{
String url = request.querystring["url"] = = null? "": request.querystring["Url"]. ToString ();
Response.Redirect (URL);
}
#endregion

After debugging, the URL gets a total of empty strings, that is, the URL is always "", so always return to the main interface.

After checking the information, I changed the procedure to read:

Copy CodeThe code is as follows:
Code added in the page load event
protected void Page_Load (object sender, EventArgs e)
{
if (! IsPostBack)
{
/****** The following code must be placed to determine whether it is a postback event, otherwise it will not be effective ******/
if (request.urlreferrer!= null)
{
viewstate["Retu"] = Request.UrlReferrer.ToString ();
}
}
}

#region返回按钮
protected void Btnreturn_click (object sender, EventArgs e)
{
String url = viewstate["Retu"]. ToString () = null? "": viewstate["Retu"]. ToString ();
Response.Redirect (URL);

}

The modified code, after debugging, url = http://localhost:18888/Order/UnfilledOrdersList.aspx, that is, the address of the main interface, so can return to the main interface correctly.

Procedure explanation: The user requests the page through the client browser, the page runs for the first time, the statement "viewstate[" Retu "] =request.urlreferrer.tostring ();" Gets the URL of the previous page of the request. The statement is placed in the IF (! IsPostBack) {} ' statement is fast because when a user enters information, selects from an option, or clicks a button, the page may be sent again to the Web server, which is called a postback in asp.net. More accurately, the page is sent to itself. So the statement "viewstate[" Retu "] =request.urlreferrer.tostring ();" The IsPostBack property of the Page object is used to avoid unnecessary processing of a round trip if the page is to be executed on the first request, not every postback.

In the "If" (! IsPostBack) {} "You can see this process clearly when you are debugging at a breakpoint.

Episode: The difference between LinkButton and hyperlink in asp.net

Because the existence of the main interface to the problem of sub-interfaces, need links, LinkButton and hyperlink can be implemented, check some information, and finally I chose hyperlink, because it is simply a jump without server-side processing. Here are the differences between LinkButton and hyperlink:
1) LinkButton support postback, processing page jump function on the server side, navigating the user to the target URL. So you can do some processing in front of the link to a new page, enter the validation input, combine into a new URL, and so on. Hyperlink will not be sent back to the server, and cannot be processed by the client.

2) LinkButton Control Implementation page jump is in the click event using Response.Redirect and other methods to achieve. Hyperlink only need to set up NavigateUrl can achieve page jump,
The biggest area in usage is LinkButton there is a click event, and Hyperlink is not.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.