ASP. NET page values: 5

Source: Internet
Author: User
Tags server memory

1. Use querystring variable

Querystring is a simple method for transferring values. It can display the transmitted values in the address bar of a browser. This method can be used to transmit one or more numeric values with low security requirements or simple structure. However, this method cannot be used to pass arrays or objects. The following is an example:

 


      A. aspx C # code private void button#click (Object sender, system. eventargs e) {string s_url; s_url = "B. aspx? Name = "+ label1.text; response. redirect (s_url);} B. in aspx, C # code private void page_load (Object sender, eventargs e) {label2.text = request. querystring ["name"];}

2. Use the application object variable

The scope of the Application object is global, that is, it is valid for all users. The common methods are lock and unlock.

 

 


      A. aspx C # code private void button#click (Object sender, system. eventargs e) {application ["name"] = label1.text; server. transfer ("B. aspx ");} B. in aspx, C # code private void page_load (Object sender, eventargs e) {string name; application. lock (); name = application ["name"]. tostring (); application. unlock ();}

3. Use session Variables

Presumably, this is definitely the most common usage. Its operations are similar to those of the application, which act on individual users. Therefore, excessive storage will result in the depletion of server memory resources.

 

 


      A. aspx C # code private void button#click (Object sender, system. eventargs e) {session ["name"] = label. text;} B. in aspx, C # code private void page_load (Object sender, eventargs e) {string name; name = session ["name"]. tostring ();}

4. Use cookie object variables

This is also a common method. Like session, it is for every user, but there is a fundamental difference, that is, cookies are stored on the client, session is stored on the server. In addition, the use of cookies should be used in combination with the ASP. NET built-in Object Request.

 


      A. aspx C # code private void button#click (Object sender, system. eventargs e) {httpcookie cookie_name = new httpcookie ("name"); cookie_name.value = label1.text; reponse. appendcookie (cookie_name); server. transfer ("B. aspx ");} B. in aspx, C # code private void page_load (Object sender, eventargs e) {string name; name = request. cookie ["name"]. value. tostring ();}

5. Use the server. Transfer Method

This can be said to be the method used for face object development. It uses server. the transfer method directs the process from the current page to another page. The new page uses the response stream of the previous page. Therefore, this method is completely object-like and concise and effective.

 

 


      A. aspx C # code public string name {get {return label1.text;} private void button#click (Object sender, system. eventargs e) {server. transfer ("B. aspx ");} B. in aspx, C # code private void page_load (Object sender, eventargs e) {A newweb; // instance a form newweb = (source) context. handler; string name; name = newweb. name ;}

Many value passing Methods

I am adding

Cross-page posting

Locate the webpage where the source webpage is located in the same application, you can read the value and public attributes of the source webpage, but do not refresh the page in the browser or the previous step. This will produce unexpected results.

 

 


      Eg: aspx your name: text = "transfer" onclick = "btn_name_click"/> Cs: textbox name = (textbox) previouspage. findcontrol ("name"); // non-public member label1.text = "Welcome:" + name. text;

Previouspage itself belongs to the page class and can be referenced only when the source webpage and target webpage belong to the same Asp.net application. If the webpage is not a target published across webpages, or the previouspage attribute is not initialized in different applications, so the previouspage cannot be used to access any information and obtain the public attributes of the webpage:

 

 


      source.cs:public string UserName{get{return aa.text;}}cross-page posting.aspx:cross-page posting.cs:lab.text = previousopage.username;

// Only one previouspage can be indicated on a webpage. Otherwise, a compilation error occurs.

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.