Asp. Net

Source: Internet
Author: User

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. For example
This method can be used if one or more values with low security requirements or simple structure are passed. However,
If an array or object is passed, this method cannot be used. 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. C # code in aspx
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. Its common method is 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. C # code in aspx
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, acting on the user's individual,
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. C # code in aspx
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 essential for every user.
The difference is that the Cookie is stored on the client, and the session is stored on the server. And Cookie enabling
It must be used 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. C # code in aspx
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 by face object development. It uses the Server. Transfer method to extract the process from the current page
The new page uses the response stream of the previous page, so this method is a complete face object.
Is 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. C # code in aspx
Private void Page_Load (object sender, EventArgs e)
{
A newWeb; // instance a form
NewWeb = (source) Context. Handler;
String name;
Name = newWeb. Name;
}

6. 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 press
Refresh the browser and the previous step, as expected
Eg:
Aspx
<Div>
Your name: <asp: TextBox ID = "name" runat = "server"> </asp: TextBox>
<Asp: Button ID = "btn_name" runat = "server" PostBackUrl = "~ /Cross-Page
Posting/CrossPageTarget. aspx"
Text = "transfer" OnClick = "btn_name_Click"/>
</Div>
Cs:
TextBox name = (TextBox) PreviousPage. FindControl ("name"); // Non-public Member
Label1.Text = "Welcome:" + name. Text;
Previouspage itself belongs to the page class and only belongs to the same asp.net application on the source and target pages.
Can be referenced only when the web page is not published across web pages, or the web page is in different applications
The previouspage attribute is not initialized, so you cannot use previouspage to access any information.
Obtain the public attributes of a webpage:
Source. cs:
Public string UserName
{
Get {return aa. text ;}
}

Cross-page posting. aspx:
<% @ PreviousPageType VirtualPath = "~ /Cross-Page Posting/Source. aspx "%>
Cross-page posting. cs:
Lab. text = previousopage. username;
// Only one previousPage can be indicated on a webpage. Otherwise, a compilation error occurs.

/////// Many problems occur when using the above content. In VS2008, I cannot access the original class name. Please refer to MSDN here. You need to set in the target page

To convert the class. '% @ Reference VirtualPath = "~ /Default. aspx "%"

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.