Summary of ASP. NET cross-page value passing skills

Source: Internet
Author: User

 

1. Use querystring variable

QuerystringIt is a very simple method for transferring values. It can display the transmitted values in the address bar of the browser. If one or moreSecurityThis method can be used when the requirement is not high or the structure is simple. However, this method cannot be used to pass arrays or objects. The following is an example:

A. aspxOfC #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. aspx medium C # Code

private void page_load (Object sender, eventargs e)

{

label2.text = request. querystring ["name"];

}

2.UseApplicationObject variable

ApplicationThe scope of the object is global, that is, it is valid for all users. Common MethodsLockAndUnlock.

. aspx C # Code

private void button#click (Object sender, system. eventargs e)

{

application ["name"] = label1.text;

server. transfer ("B. aspx ");

}

B. aspx medium C # Code

private void page_load (Object sender, eventargs e)

{

string name;

application. lock ();

name = application ["name"]. tostring ();

application. unlock ();

}

3.UseSessionVariable

Presumably, this is definitely the most common usage. its operations andApplicationSimilarly, it applies to individual users. Therefore, excessive storage will leadServerMemory resource depletion.

. aspx C # Code

private void button#click (Object sender, system. eventargs e)

{

session ["name"] = label. text;

}

B. aspx medium C # Code

private void page_load (Object sender, eventargs e)

{

string name;

name = session ["name"]. tostring ();

}

4.UseCookieObject variable

This is also a common method. Session The same is true for every user, but there is an essential difference: Cookie Is stored on the client, and Session Is stored on the server side. And Cookie Must be used together ASP. NET Built-in object Request .

A. aspxOfC #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. aspx medium C # Code

private void page_load (Object sender, eventargs e)

{

string name;

name = request. cookie ["name"]. value. tostring ();

}

5.UseServer. TransferMethod

This can be said to be the method used for face object development.Server. TransferThe method directs the process from the current page to another page, and the new page uses the response stream of the previous page. Therefore, this method is completely facial and effective.

. aspx C # Code

Public string name

{

get {return label1.text ;}

}

private void button#click (Object sender, system. eventargs e)

{

server. transfer ("B. aspx ");

}

B. aspx medium C # Code

Private void page_load (Object sender, eventargs E)

{

A newweb; // instance a form

Newweb = (source) Context. Handler;

String name;

Name = newweb. Name;

}

 

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.