ASP. NET has the biggest advantage over ASP and is also receiving more and more Program ASP. NET provides an event-driven development model for developers. From then on, programmers can use visual programming tools for development. Windows ASP. NET web-based applications are developed like applications.
ASP.. net programs have at least two or more web pages, and programmers are developing web pages similar to ASP. NET applications generally need to handle the problem of data transmission on multiple web pages. We know that each web page in the ASP. NET program is similarWindowsThe Form in the application, but the web organization structure in the ASP. Net program is much loose than the form in the Windows application. Data transmission between forms in Windows applications can be achieved by defining global variables (public static variables) and other methods. However, these methods cannot be used to transmit data on various web pages in ASP. NET. Therefore, you must find another method. This article introduces two methods for transferring data from various web pages in ASP. NET:
The first method is to use querystring,
The second method is to use session.
I. The software environment for program design and operation in this article:
(1 ). Microsoft Windows 2000Server.
(2 ). Visual Studio. NetOfficial version ,. Net Framework SDK version 3705.
II. Use querystring to transfer data between web pages:
Querystring is an old method to transmit data between web pages. In ASP, this method is used to transmit data between different web pages. The biggest advantage of this method is its simplicity, but its disadvantage is also very obvious.SecurityPoor performance. when data is transferred from the source page to the target page, the data transmitted between pages will be displayed in the address bar of the target page. See the address bar in Figure 05. In addition, querystring can only transmit a limited amount of data between pages, and Object-type data cannot be transmitted. The following describes how to transfer data between ASP. NET pages. The development tool is Visual Studio.. NetThe development language is C #. The function is to pass the data in the two textbox In The Source Page (webform1.aspx) to the target page and pass the data on the target page (webform2.aspx) the two labels in show the data transferred from the source page:
1. Start Visual Studio. NET.
2. Select "file", "new", and "project". The "new project" dialog box is displayed.
3. Set "project type" to "Visual C # project 」.
4. Set "template" to "ASP. NET web application 」.
5. enter "http: // localhost/webapplication4" in the "location" text box ". click OK. net will create a folder named "webapplication4" in the directory where the current project file is located, which stores the project file of this project, other files in the project are stored in a folder named "webapplication4" created in the directory where the default web site of the computer Internet Information Service is located.
01: a new folder named "webapplication4" is created in the default web site directory.
01: This method is used to transmit data between different web pages.
The biggest advantage of this method is its simplicity, but its disadvantage is also very obvious.SecurityPoor performance. when data is transferred from the source page to the target page, the data transmitted between pages will be displayed in the address bar of the target page. See the address bar in Figure 05. In addition, querystring can only transmit a limited amount of data between pages, and Object-type data cannot be transmitted. The following describes how to transfer data between ASP. NET pages. The development tool is Visual Studio. NET and the development language is C #. The function is to pass the data in the two textbox In The Source Page (webform1.aspx) to the target page and pass the data on the target page (webform2.aspx) the two labels in show the data transferred from the source page:
1. Start Visual Studio. NET.
2. Select "file", "new", and "project". The "new project" dialog box is displayed.
3. Set "project type" to "Visual C # project 」.
4. Set "template" to "ASP. NET web application 」.
5. enter "http: // localhost/webapplication4" in the "location" text box ". click OK. net will create a folder named "webapplication4" in the directory where the current project file is located, which stores the project file of this project, other files in the project are stored in a folder named "webapplication4" created in the directory where the default web site of the computer Internet Information Service is located. 01: a new folder named "webapplication4" is created in the default web site directory. 01: a new folder named "webapplication4" is created in the default web site directory. 01: a new folder named "webapplication4" is created in the default web site directory. 01:
Figure 01: Create an ASP. NET project dialog box
6. click project> Add web form. In the Add new project dialog box that appears, set template to web form ], enter "webform2.aspx" in the "name" text box and click "open". A new form "webform2.aspx" is added to this project, as shown in 02:
Figure 02: Add new item dialog box
7. set Visual Studio. the current page of net is set to the design interface of the webform1.aspx page, drag the following components to the webform1.aspx page from the web forms tab in the toolbox, and perform the corresponding operations:
The two textbox components are used to input the data passed to the target form.
A button component is used to transfer data from the source page to the target page. After the button component is dragged to the webform1.aspx design page, double-click it. Then the system will go to webform1.aspx .. processing corresponding to the Click Event of the component automatically generated in the CS FileCode. Page 03 after webform1.aspx design:
Figure 03: webform1.aspx design page
8. set Visual Studio. the current page of net is set to the design interface of the webform2.aspx page, drag the following components to the webform2.aspx page from the web forms tab in the toolbox, and perform the corresponding operations:
Two label components are used to display the data transmitted from the source page to the target page.
Double-click the webform2.aspx design page. The system will process the code corresponding to the load event of the automatic page in the webform2.aspx .. CS file. In this event, the program receives the data transmitted from the source page to the target page and displays it through the lable component. Page 04 after webform2.aspx design:
Figure 04: webform2.aspx design page
9. Switch the current window of Visual Studio. NET to the code editing window on the webform1 page, that is, the editing window of the webform1.aspx. CS file. Use the following code to replace the processing code corresponding to the Click Event of the button1 component in webform1.aspx. CS:
Private Void Button#click ( Object Sender, system. eventargs E)
{
String Surl; // Defines a string that contains the data transmitted from the source page to the target.
Surl = " Webform2.aspx? Name = " + Textbox1. Text + " & Email = " + Textbox2. text; // Obtain transmitted data from the Source Page
Response. Redirect (Surl ); // Transmit data to the target page
}
10. Switch the current window of Visual Studio. NET to the code editing window on the webform2 page, that is, the editing window of the webform2.aspx. CS file. Use the following code to replace the processing code corresponding to the page load event in webform2.aspx. cs. The following code is used to receive data from the source page and display the received data through the corresponding components:
Private Void Page_load ( Object Sender, system. eventargs E)
{
Label1. Text = Request. querystring [ " Name " ]; // Use querystring to receive name variable data from the source page and display it through label1
Label2. Text = Request. querystring [ " Email " ]; // Use querystring to receive email variable data from the source page and display it through label2
// Place user code here to initialize the page
}
11. so far, the first implementation of ASP. NET page data transmission method is described, then click the shortcut key [F5] to run the program, and in the first page, the two textbox components are respectively input "tiger", "ahah@etang.com", after the specific 05, and then click the button1 button in the page, the page shown in Figure 06 is displayed. The data on the Source Page is indeed transferred to the target page:
Figure 05: Running page 01
Figure 06: Running page 02
3. Use session to transfer data between web pages:
Session is a concept that is often encountered when writing web pages. Although the use of session to transmit data can overcome the disadvantages of querystring, a large amount of use may lead to server paralysis, especially for pages with large page views, pay special attention to the use of sessions. The solution is to clear the session after it is used. The following is a general procedure for transferring data using session in ASP. NET. The development tool is Visual Studio. NET and the language is C #:
1. Start Visual Studio. NET.
2. Select File, new, and project. The new project dialog box is displayed.
3. Set project type to Visual C # project ].
4. Set template to ASP. NET web application ].
5. Enter "http: // localhost/session" in the "location" text box ". Click OK. net will create a folder named "session" in the directory where the current project file is located, which stores the project files of this project, other files in the project are stored in a folder named "session" created in the directory of the default web site of the computer Internet Information Service. Specific 07:
Figure 07: Create an ASP. NET project dialog box
6. Follow the sixth step in the querystring implementation step to create a new web page named "webform2.aspx" in the session project ".
7. follow Step 7 in the querystring implementation step. On the webform1.aspx page, create two textbox components and one button component, and go to webform1.aspx. the processing code corresponding to the Click Event of the button component created in CS.
8. Follow the eighth step in the querystring implementation step to create two label components on the webform2.aspx page, and create the processing code for the load event on this page in webform2.aspx. CS.
9. Switch the current window of Visual Studio. NET to the code editing window on the webform1 page, that is, the editing window of the webform1.aspx. CS file. Use the following code to replace the processing code corresponding to the Click Event of the button1 component in webform1.aspx. cs. The function of the following code is to use session to transmit data to the target page on the Source Page:
Private Void Button#click ( Object Sender, system. eventargs E)
{
Session [ " Name " ] = Textbox1. text; // Creates a session variable to store data in the textbox1 component.
Session [ " Email " ] = Textbox2. text; // Creates a session variable to store data in the textbox2 component.
Server. Transfer ( " Webform2.aspx " ); // Transmits data to the target page. If you are on another page, replace the preceding webform2.aspx
}
10. Switch the current window of Visual Studio. NET to the code editing window on the webform2 page, that is, the editing window of the webform2.aspx. CS file. Use the following code to replace webform2.aspx. the processing code corresponding to the load event of page in CS. The following code is used to receive data from the Source Page, display the received data through the corresponding component, and clear the created session variable:
Private Void Page_load ( Object Sender, system. eventargs E)
{
Label1. Text = Session [ " Name " ]. Tostring ();
Label2. Text = Session [ " Email " ]. Tostring (); // Receives and displays data from the source page.
Session. Remove ( " Name " );
Session. Remove ( " Email " ); // Clear the created session variable
// Place user code here to initialize the page
}
11. Now, ASP. NET uses session to implement data transfer between web pages. Click the shortcut key [F5] to run the program, and the running interface shown in Figure 05 and figure 06 is displayed.
Iv. Summary:
This article introduces two methods for transferring data between web pages in ASP. NET: querystring and session. The two methods have insurmountable shortcomings in QIQIU. In summary, querystring is quite simple, and the session is relatively complex. querystring has poor security and high Session Security. querystring can transmit simple data on various web pages, which is generally a string type, session can transfer complex types of data between web pages. Of course, in addition to the above methods, the methods for transferring web page data in ASP. NET may also have other better and simpler methods. If you have the opportunity, you may wish to discuss them together.