ASP.NET returns the implementation code of the previous page

  • 2020-05-12 02:28:14
  • OfStack

Solutions:

Procedures related to the introduction

Main interface: no shipment order list (http: / / localhost: 18888 / Order UnfilledOrdersList aspx)

Child interface: order details (http: / / localhost: 18888 / Order ViewOrderDetail aspx? OrderId= id, where id value is id of the order information selected in the main screen)

The main page is the main information of an order. There is an HyperLink control in GridView, through which you can jump to the "order details" interface to view the details of the order.

The subinterface has a "return" Button to jump back to the main interface.

The original program in the back button is:
 
#region  Back 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 value of url is always an empty string, that is, url is always "", so it never returns to the main interface.

After checking the information, I changed the procedure to:

 
// The code added in the page load event  
protected void Page_Load(object sender, EventArgs e) 
{ 
if (!IsPostBack) 
{ 
/****** The following code 1 Must be put in the back event to determine whether or not it will not work ******/ 
if (Request.UrlReferrer != null) 
{ 
ViewState["retu"] = Request.UrlReferrer.ToString(); 
} 
} 
} 

#region Back button  
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, namely the address of the main interface, so correctly returns to the main interface.

The user requests the page through the client browser, and the page is run for the first time with the statement "ViewState["retu"] = Request.UrlReferrer.ToString ();" The URL of the last page of the request is obtained. This is placed in "if (! The IsPostBack){}" statement is fast because when the user enters information, selects from the options, or clicks a button, the page may be sent to the Web server again, which is called "postback" in ASP.NET. More specifically, the page sends itself. Therefore, the statement "ViewState["retu"] = Request.UrlReferrer.ToString ();" Simply execute it on the first request for the page, rather than each postback, and use the IsPostBack property of the Page object to avoid unnecessary processing of the round-trip trip.

In "if (!!! IsPostBack){}" this process can be clearly seen when debugging the breakpoint.

Interlude: the difference between LinkButton and HyperLink in ASP.NET

Because of the problem of the main interface oriented jump, the link is needed. LinkButton and HyperLink can be realized. After looking up some information, I finally chose HyperLink, because it is a simple jump without any server-side processing. Here is the difference between LinkButton and HyperLink:
1) LinkButton supports postback, handles the page jump function on the server side, and navigates the user to the target URL. So you can do some processing before you link to a new page, check in the input, combine it into a new URL, etc. HyperLink will not be sent back to the server and cannot be processed on the server side.

2) LinkButton control realizes page jump by using Response.Redirect and other methods in Click event. HyperLink simply sets NavigateUrl to make a page jump,
The largest area in usage is the LinkButton area with the Click event and the HyperLink area with the Click event.

Related articles: