Asp. NET between pages in __.net

Source: Internet
Author: User

ASP. NET-pass values between pages in

One, for example

Taobao, enter the correct username, password can be logged into the personal account of the page, such as the user name Wang Wang. Users want to search for goods, view products, view personal information, will open a lot of web pages. At this time, users want to do all the operations are based on their own account. In other words, regardless of the user Wang Wang open how much Taobao Web page, as long as the account does not quit, is in their own account under the operation.

In this case, it involves the transfer between the value of the page, that is, users want to open the page is marked as "Wang Wang" page.

Second, ASP. The technique of value-transfer between Web pages used in net

Technology one: Request. QueryString ["] Way

Technique Two: Session object

rule: Regardless of which method is used, the value must be divided into two steps: The first step is to assign a value to the first page, and the second to receive the passed value in the second Web page.

third, Request. QueryString ["] Way

Using this method to pass the value is implemented through the URL.

Example: Step one: First in the first page. Assignment in ASPX

Response.Redirect ("second.aspx?id=100");

Explain:

The Response.Redirect () method is linked to another Web page

Second.aspx refers to the target page.

。 Indicates the value of the transfer between pages

ID=100 indicates that the actual value to be passed is 100, which is referred to by the name ID (ID is the name that can be named according to the actual situation).

If the message you want to pass is a username, you can write

Response.Redirect ("Second.aspx?name= Wang Wang");

Note: Wang Wang does not use single quotes to generate.

Step two: Receive the passed value in the second page second.aspx

Case one: string str = request.querystring["id"];//that is str= "100"

or string str = request.querystring["name"];//that str= "Wang Wang"

In this way, the value passed from the First.aspx Web page is received in the Second.aspx page by variant str of type String.

Case two: The value received is directly used in the SQL statement.

Law: The General page between the transfer value is used in SQL statements, so to master this method.

(1) String strSQL = "SELECT * FROM news where id =" +request. QueryString ["id"];

The actual SQL statement is: SELECT * from news where ID =100;//prerequisite: Field ID is integral type

(2) String strSQL = "SELECT * FROM news where name = '" +request. QueryString ["name"] + "'";

The actual SQL statement is: SELECT * FROM news where name = ' Wang Wang '/Prerequisite: Field name is character type

four, Session object

Using the Session object is equivalent to defining a "global variable" within the entire site, and you can pass the value in the Web page.

Note: Web pages that use the session object are in a sequential order. Must be the "declare" Session object's Web page run first.

Example:

Step one: Declare the Session object in the First.aspx Web page

protected void Page_Load (object sender, EventArgs e)

{

session["Name"] = "";

The equivalent of declaring a "global variable" that can pass values between pages

}

protected void Button1_Click (object sender, EventArgs e)

{

session["Name" = "Wang Wang"; Assign a value to the Session object name

Response.Redirect ("second.aspx");//link to another Web page

}

Step two: Use the Session object in the Second.aspx Web page

protected void Page_Load (object sender, EventArgs e)

{

if (session["Name"] = = "Wang Wang")

{

label1.text= "User Wang Wang: Good morning." ";

}

Else

{

Response.Redirect ("first.aspx"); If the value of the session object name is not "Wang Wang", then the page jumps directly into the first.aspx to protect the Web page information.

}

}

Note: When you use the Session object, you must first run the page from the First.aspx page. If you start from Second.aspx, the Session object name does not have a declaration and is not assigned a value, and an 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.