Methods for passing parameters between ASP.

Source: Internet
Author: User

Method http://www.cnblogs.com/eoiioe/archive/2008/04/08/1142247 for passing parameters between ASP. This new feature of HTML means that asp.net2.0 developers currently have three alternative technologies to transfer data from one Web page to another. These three methods are: Response redirection, server-side transfer, and new cross-page commit features.   We can already be familiar with the first two technologies, so we'll just review them briefly, and then we'll focus on learning how to use cross-page submission features, and how this approach differs from response redirection and service delivery. One, the response redirection method response redirection method is the simplest way to redirect one Web page to another page so far. When the Web server receives a redirect request, it sends a response header to the client, which causes the client to send a new request to the server.  In other words, a redirect request is actually two request responses: one is the initial request response and the other is the new Redirect request response. It is easy to implement redirection in ASP. The following code demonstrates how to use the Response.Redirect method to implement Web page redirection: protected void Redirect_click (object sender, EventArgs e) {Response.redire   CT ("menu.aspx"); Note that the redirect request is just a GET request, which means that we cannot submit the data from the source page through the redirect command. But we can use the query string to pass the data in the redirect. As shown in the following code: protected void Redirect_click (object sender, EventArgs e) {Response.Redirect ("menu.aspx?username=" + Userna Me. Text)); The above example passes a query string as a parameter to the destination URL of the Response.Redirect method. We can get the source data by the following code. protected void Page_Load (object sender, EventArgs e) {string userName = request["UserName"];} second, server transfer method and dependent on client to another new page Requests, server transport is a service redirection technology that simply changes the webThe server handles the code to achieve the purpose of requesting a new page. When the requested page and the source page are on the same server, the server transfer is more efficient than the Response.Redirect method, because the technology avoids the extra overhead and can be redirected simply by using the server's resources. One of the side effects of this technique is that when the page is redirected, the client's URL remains the URL of the source page, which may cause the customer to assume that the data they have obtained is generated from the source page.  Of course, this is not a problem in most cases, but it makes debugging more difficult. The Server.Transfer method can also save the HttpContext of the initial page. Therefore, the target page can access the value of the source page. We can use the FormsCollection property to get the value of the source page from the target page. First, to make sure that we used the overloaded method, this method has two parameters: the target URL and a Boolean value that tells the server whether to save the form that describes the source page value.   As shown in the following code: Server.Transfer ("menu.aspx", true);   We then get the code for the value of a TextBox control called txtUserName in the target page as follows: Object obj = request.form["txtUserName"]; Comparison of Response.Redirect and Server.Transfer because the Response.Redirect method requires two request response operations, we should try to avoid using this method on sites with high performance requirements. However, only technically, using the Redirect method can actually jump from one Web page to another. In contrast, Server.Transfer will be more efficient, but the range of jumps is limited to different pages of the same Web server. In essence, we can use Server.Transfer to eliminate unnecessary request-response operations.  If we need to relocate to a different server page, we need to use the Response.Redirect method. Iv. Overview of cross-page submissions in ASP. NET 2.0, we can implement cross-page submissions by implementing IButtonControl interfaces to different WebForm. Similar to Response.Redirect, cross-web submissions are a client-based transport mechanism, but also somewhat like Server.Transfer, where the target Web page can also access data from the source Web page.  In order to use cross-page submissions, we need to specify the destination URL in the PostBackUrl property in the source Web page. V. Implementation of cross-page submissions this section willTalk about how to implement cross-page submissions in asp.net2.0. To begin our study, suppose there are two Web pages, one source Web page and the other is the target Web page. Cross-page commit operations using buttons are initialized in the source Web page. We must first set the PostBackUrl property of the target page button, and by the way, all Web controls that implement the System.Web.UI.WebControls.IbuttonControl interface have features that cross-page commits.   The following code demonstrates this process. Postbackurl= "~/target.aspx" text = "Post to a target page"/> When we set the PostBackUrl property, ASP. NET Framework binds the corresponding control to a new JavaScript function called Webform_dopostbackwithoptions, resulting in the following HTML code: onclick= "Javascript:webform_ Dopostbackwithoptions (New Webform_postbackoptions ("Btnsubmit", "" ", False," "," target.aspx ", False, False))" Id= " Btnsubmit "/> For the above HTML code, when the user clicks the button, the browser submits the destination URL (target.aspx) instead of the source URL. Vi. getting the value of the source page control from the target page asp.net2.0 provides a new property called PreviousPage, which points to the source page whenever the current page commits a cross-page commit operation. It is important to note that when the source and destination pages are in different applications, this property contains null (this null is not an uninitialized meaning). It is also important to note that when the target page accesses the PreviousPage property, the data from the source page can be obtained, ASP. The net runtime loads and executes the source page. This will cause the Processchildrequest event to occur.  Also, it raises Page_Init events, Page_Load, and any other source page button click events. Therefore, we want to avoid careless operation by mistake, so it is best to confirm whether a cross-page commit occurs with the Iscrosspostback property, if this property value is true, then the target page is called through a cross-page commit action. If it is called in a different way (such as a general request, Response.Redirect, or an SERVer. Transfer), the value of this property is false. The following example shows how to use this property. if (previouspage.iscrosspagepostback) {//Execute code}//****************************************** this PreviousPage property in serve R.transfer and cross-web submissions are available. In asp.net2.0, we can use the PreviousPage property on the target page to get the data from the source page after invoking the Server.Transfer operation, with the following code: ************************************    {Server.Transfer ("menu.aspx"); } protected void Redirect_click (object sender, EventArgs e)//In this receive polygon we can now get the data for the Web page, the code is as follows: protected void Page_Load (O Bject sender, EventArgs e) {if (previouspage! = null) {textbox txtbox = (TextBox) previouspage.findcontrol ("Tx    Tusername ");    if (TextBox = null) string userName = TextBox.Text; Other executable code} It is important to note that the above code must convert the txtUserName control to a TextBox type so that its values can be accessed. Vii. using the PreviousPageType PreviousPageType property provides a strong typing ability to access the source page in cross-page operations, let's show how to get control values from the source page without any type conversion. The code is as follows: < Asp:textbox id= "txtUserName" runat= "Server"/> < asp:textbox id= "Txtpassword" runat= "Server"/> &lt ; Asp:button id= "Submit" runat= "Server" text="Login" postbackurl= "menu.aspx"/> Note that the click button can be redirected to a target page called "menu.asp". This target page can use the following code to get the user name and password: viii. save view state for cross-web submissions, ASP. NET2.0 embedded a hidden field called __postback that contains the view information about the source page-that is, provided by the source page, which contains a server-side control with a non-null PostBackUrl property value. The target page can use the information in __postback to get the view state information for the source page. The code is as follows: if (previouspage!=null && previouspage.iscrosspagepostback && previouspage.isvalid) {TextBox T    Xtbox = Previouspage.findcontrol ("txtUserName"); Response.Write (Txtbox.text); The check code used to ensure that the PreviousPage property is not NULL is checked in the code above. By the way, if the target page and the source page are not in the same application, the value of this previouspage property is null.  The Iscrosspagepostback property is true only if the cross-page commit operation is in progress. This cross-page commit feature is one of the strongest features in asp.net2.0, which allows you to submit a page to another page and seamlessly manipulate the data on the source page on the target page.

How to pass parameters between ASP. (GO)

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.