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; } |