Asp.net| Data | page
Asp. NET is one of the most important advantages of ASP, and the main reason why it is welcomed by more and more programmers is that ASP. NET provides developers with event-driven development patterns from which programmers can develop asp.net web-based applications just as they would with visual programming tools to develop Windows applications.
Typically a slightly larger ASP.net program has at least two or more Web pages, and programmers typically need to handle the problem of passing data across multiple Web pages when developing similar asp.net applications. We know that individual web pages in the asp.net program resemble form forms in Windows applications, but the organizational structure of the web in the ASP.net program is much looser than the form forms in Windows applications. Data transfer between form forms in a Windows application can be implemented by defining a global variable (public static variable). But these methods can not be used to asp.net the Web page data transmission, so we must find another way. This article introduces two kinds of methods that can realize the data transmission of each Web page in asp.net: The first method is to use QueryString, and the second method is to use session.
one. The design and operation of this paperSoftware Environment:
(1). Microsoft Windows 2000 Server Edition.
(2). Visual Studio. NET official edition,. NET FrameWork SDK version number 3705.
two. Using QueryString to implement data transfer between Web pages:
QueryString is an older way to implement data transfer between Web pages, which is used in ASP to implement data transfer between different Web pages. The greatest advantage of this approach is simplicity, but its disadvantage is very obvious, that is, its security is relatively poor, the specific performance of the data from the source page to the target page, the data passed between the pages will be displayed in the address bar of the target page, the specific visible figure 05 in the Address bar. There is also the use of QueryString to pass only a limited amount of data between pages, and it is not possible to pass data of type object. The following is the specific implementation steps for data transfer between ASP.net pages, using visual Studio. Net, and the development language is C #. The function is to pass data from the two textbox in the source page (WebForm1.aspx) to the target page and display the data passed from the source page through two labels in the target page (webform2.aspx):
1. Start visual Studio. Net.
2. Select Menu "File" | "new" | "Project", pop-up the "New Project" dialog box.
3. Set the project type to Visual C # project.
4. Set "template" to "asp.net Web application".
5. In the "Position" text box, enter "Http://localhost/WebApplication4". Then click the OK button so that you are in visual Studio. NET creates a "WebApplication4" folder in the directory where the current project file is located, containing the project file for this project, Other files in the project are located in a new folder named "WebApplication4" in the directory where the default Web site for computer Internet Information Services resides. As shown in Figure 01:
Figure 01: Create a new asp.net Project dialog box
6. Click menu "Project" | "Add Web Form", the Add New Item dialog box pops up, the dialog template is set to Web form, and when you enter "Webform2.aspx" in the Name text box and then click the Open button, another new form is added to this item. Webform2.aspx ", as shown in Figure 02:
Figure 02: Add New Item dialog box
7. Take visual Studio. NET is set to the design interface of the WebForm1.aspx page, and from the Web Forms tab in the Toolbox, drag the following components to the WebForm1.aspx page and do the appropriate action:
two textbox components, Use to enter data to be passed to the target form, respectively.
A button component that enables the source page to pass data to the destination page and, when the button component is dragged into the WebForm1.aspx design page, double-clicks on it, the system is WebForm1.aspx. The CS file automatically produces this component's Click event corresponding to the processing code. The WebForm1.aspx design page looks like Figure 03:
Figure 03:webform1.aspx page after design
8. Take visual Studio. NET is set to the design interface of the Webform2.aspx page, and from the Web Forms tab in the Toolbox, drag the following components to the Webform2.aspx page and do the appropriate action:
Two label components, Used to display the data that the source page passes to the destination page.
Double-click the webform2.aspx design page, and the system will be webform2.aspx ... The processing code corresponding to the Load event for the automatic page page in the CS file. In this event, the program receives the data from the source page to the target page and displays it through the lable component. The webform2.aspx design page looks like Figure 04:
Figure 04:webform2.aspx page after design
9. Take Visual Studio. NET switch to the Code editing window of the WebForm1 page, that is, the edit window for the WebForm1.aspx.cs file. Replace the processing code for the Click event of the Button1 component in WebForm1.aspx.cs with the following code, which is the function of implementing the source page to pass data to the destination page:
private void Button1_Click (object sender, System. EventArgs e) { String sURL; Defines a string that contains data that the source page passes to the target sURL = "Webform2.aspx?name=" + TextBox1. Text + "&email=" + TextBox2. Text; Get the data passed from the source page Response. Redirect (sURL); Passing data to the destination page } |
10. Take visual Studio. NET switch to the Code editing window of the WebForm2 page, that is, the edit window for the WebForm2.aspx.cs file. Replace the processing code for page load events in WebForm2.aspx.cs with the following code, which is the function of receiving data from the source page and displaying the received data through the corresponding component:
private void Page_Load (object sender, System. EventArgs e) { Label1. Text = Request. QueryString ["Name"]; Use QueryString to receive the name variable data from the source page and display it by Label1 Label2. Text = Request. QueryString ["Email"]; Use querystring to receive email variable data from the source page and display it via Label2 Place user code here to initialize page } |
11. This is the first way to implement data transfer between ASP.net pages, the introduction of the method, click the shortcut "F5" Run the program, and in the first page of the two TextBox components entered the "Tiger", "ahah@etang.com", specifically as shown in Figure 05, Then click the Button1 button on the page, and you'll get the page shown in Figure 06, which actually delivers the data from the source page to the target page:
Figure 05: Run page 01
Figure 06: Run Page 02
three. Use session to implement data transfer between Web pages:
Session sessions are the concepts often encountered when writing Web pages, while using session to pass data can overcome the disadvantage of using querystring, but a large number of uses may cause the server to be paralyzed, especially the page with very large browsing volume, especially when using session. The solution is to use the session after the end, must be cleared. Here's a general step to passing data using session in ASP.net, using the development tools Visual Studio. Net, and the language is C #:
1. Start visual Studio. Net.
2. Select Menu "File" | "new" | "Project", pop-up the "New Project" dialog box.
3. Set the project type to Visual C # project.
4. Set "template" to "asp.net Web application".
5. In the "Position" text box, enter "Http://localhost/session". Then click the OK button so that you are in visual Studio. NET creates a name called the "session" folder in the directory where the current project file resides, where the project file for this project is located, and the other files in the project are located in the directory where the default Web site for the computer Internet Information Service is located is a new name called " folder in the session. As shown in Figure 07:
Figure 07: Create a new asp.net Project dialog box
6. Follow the sixth step in the QueryString implementation step to create a new Web page in the session project named "Webform2.aspx".
7. Follow the seventh step of the QueryString implementation step to create two TextBox components, a button component, in the WebForm1.aspx page. And in WebForm1.aspx.cs, create the processing code for the Click event for this button component.
8. Follow the eighth step of the QueryString implementation step to create two label components in the Webform2.aspx page and create the processing code for the Load event for this page in WebForm2.aspx.cs.
9. Take visual Studio. NET switch to the Code editing window of the WebForm1 page, that is, the edit window for the WebForm1.aspx.cs file. Replace the processing code for the Click event of the Button1 component in WebForm1.aspx.cs with the following code, which is the function of passing data to the destination page using the Session Implementation source page:
private void Button1_Click (object sender, System. EventArgs e) { session [' name '] = TextBox1. Text; Create a session variable to hold the data in the TextBox1 component session ["Email"] = TextBox2. Text; Create a session variable to hold the data in the TextBox2 component Server. Transfer ("webform2.aspx"); Pass data to the target page and replace the webform2.aspx if you are a different page } |
10. Take visual Studio. NET switch to the Code editing window of the WebForm2 page, that is, the edit window for the WebForm2.aspx.cs file. Replace the processing code for page load events in WebForm2.aspx.cs with the following code, which is the ability to receive data from the source page, display the received data through the corresponding component, and clear the session variable that was created:
private void Page_Load (object sender, System. EventArgs e) { Label1. Text = Session [' Name ']. ToString (); Label2. Text = Session ["Email"]. ToString (); Receive data from the source page and display it Session. Remove ("name"); Session. Remove ("email"); To clear a variable that creates a session Place user code here to initialize page } |
11. At this point in the ASP.net use session to implement data transfer between the Web page is finished, then click the shortcut "F5" Run the program can be as shown in Figure 05 and figure 06 of the running interface.
Four. Summary:
This paper introduces two kinds of methods for implementing data transfer between Web pages in asp.net, namely: QueryString and Session. These two methods can be overcome by their own shortcomings. Summed up is QueryString is quite simple, the session is relatively complex; QueryString security is poor, session security is high; QueryString can achieve simple data delivery for each Web page, usually the string type, Session enables the delivery of complex types of data between Web pages, and so on. Of course, the method of implementing Web page data transmission in ASP.net in addition to the above methods, there are certainly other better and simpler, if there is a chance, hope to be able to explore together.