Asp.net Parameters

Source: Internet
Author: User
Asp.net Parameters

ASP. NET provides an excellent event-driven programming model, allowing developers to simplify the overall design of the application, but this also causes some inherent problems, such as the use of traditional ASP, by using the POST method, we can easily transfer values between pages. Similarly, when using the event-driven programming model ASP. net is not that easy. Of course, we still have some methods to implement the same function. This article will try to solve this problem using different possible methods, but it is foreseeable that this article will include the querystring, session variable, and server. Transfer Method for cross-page value transfer.
Using querystring to use querysting to pass values between pages is a very old mechanism. The main advantage of this method is that it is very simple to implement, however, its disadvantage is that the passed value is displayed on the address bar of the browser (Insecure), and the object cannot be passed, however, this method is a good solution when the number of transmitted values is small and the security requirements are not high. The steps for using this method are as follows: 1. Use the control to create a web form (Form) 2. Create a button that can return a form and a link button 3, create a character variable 4 to save the URL in the button or link button click event, and add the querystring parameter to the saved URL.
5. Use response. the code snippet below redirect redirects to the saved URL demonstrates how to implement this method: Source Page code: Private void button#click (Object sender, system. eventargs e) {string URL; url = "anotherwebform. aspx? Name = "+ textbox1.text +" & Email = "+ textbox2.text; response. Redirect (URL );
} Target Page code: Private void page_load (Object sender, system. eventargs e) {label1.text = request. querystring ["name"]; label2.text = request. querystring ["email"];} using session variables is another way to pass values between pages, in this example, we store the value in the control in the session variable and use it on another page to transfer the value between different pages. However, it should be noted that storing too much data in the session variable will consume a lot of server resources, so you should be careful when using the session. Of course, we should also use some cleanup actions to remove unnecessary sessions to reduce unnecessary resource consumption. The general steps for transferring values using session variables are as follows:
1. Add necessary controls 2 to the page, and create buttons and link buttons that can return to the form. 3. click the button or link button in the event, add the control value to session variable 4 and use response. the redirect method redirects to another page 5 and extracts the session value from another page, the following code snippet to explicitly clear it demonstrates how to implement this method: Source Page code: Private void button#click (Object sender, system. eventargs e ){
// Textbox1 and textbox2 are webform // controls session ["name"] = textbox1.text; session ["email"] = textbox2.text; server. transfer ("anotherwebform. aspx ");} target Page code: Private void page_load (Object sender, system. eventargs e) {label1.text = session ["name"]. tostring ();
Label2.text = session ["email"]. tostring (); Session. remove ("name"); Session. remove ("email");} Use server. the transfer method is a little more complex than the method described above, but it is particularly useful in inter-page value transfer, using this method, you can access the exposed value in the form of Object Attributes on another page. Of course, this method is used, you need to write additional code to create some attributes so that you can access it on another page. However, the benefits of this method are also obvious. In general, this method is concise and object-oriented. The entire process of using this method is as follows:
1. Add necessary controls on the page. 2. Create the get attribute of the returned value. 3. Create the buttons and links that can return the form. 4. Click the buttons to call server in the event handler. transfer the transfer method to the specified page 5. On the second page, we can use context. the handler property is used to obtain the reference of the previous page instance object. With it, you can use the value of the control to access the previous page. The following code comprehensively implements the code of the above step process: source Page code: Add the following code to the page: Public string name {get {return
Textbox1.text ;}} Public String email {get {return textbox2.text ;}} then calls server. transfer Method private void button#click (Object sender, system. eventargs e) {server. transfer ("anotherwebform. aspx ");} target Page code: Private void
Page_load (Object sender, system. eventargs e) {// create instance of source web form webform1 WF1; // get reference to current handler instance WF1 = (webform1) context. handler; label1.text = wf1.name; label2.text = wf1.email;} Summary This article describes how to implement ASP using different methods. NET page value transfer, the three methods are: querystring, session and server. transfer, we should repeat the similarities and differences between several methods
  

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.