.NET how to implement parameters between the page

xiaoxiao2021-03-05  21

Use querystring

Using QuerySting to pass the value between the page is already a very old mechanism, the main advantage of this method is to achieve

Often simple, however, its disadvantage is that the value of the transfer is displayed on the address bar of the browser (insecure), and cannot pass

In the case of the hand, this method is still a good solution if the value is less than the value of the security. The steps to use this method are as follows:

1. Create a Web Form (Form) using the control

2. Create buttons and link buttons that can return forms

3. Create a character variable that saves the URL in the click event of the button or link button.

4, add querystring parameters in the saved URL

5. Redirection to the URL saved above using Response.Redirect

The following code snippet demonstrates how to implement this method:

Source page code:

Private void button1_click

(Object Sender, System.EventArgs E)

{

String URL;

URL = "ANOTHERWEBFORM.ASPX? Name =" TextBox1.text "& email =" TextBox2.Text;

Response.Redirect (URL);

}

Target page code:

PRIVATE VOID PAGE_LOAD

(Object Sender, System.EventArgs E)

{

Label1.text = Request.QueryString ["name"];

Label2.text = Request.QueryString ["email"];

}

Use session variables

Using the session variable is another way to pass the value between the page, in this case, we exist in the values ​​in the control.

In the session variable, then use it in another page to implement the purpose of value between different pages. However, need

Note that excessive data stored in session variables will consume more server resources, should be cautious when using session

Heavy, of course, we should also use some cleaning action to remove some unwanted session to reduce resources.

Consumption. The general steps of using the session variable transfer value are as follows:

1, add the necessary controls in the page

2. Create buttons and link buttons that can return forms

3. In the click event of the button or link button, add the value of the control to the session variable.

4, use the response.redirect method to redirect to another page

5. Extract the value of the session on another page, and explicitly clear it when you are not required to use the session.

The following code snippet demonstrates how to implement this method:

Source page code:

Private void button1_click

(Object Sender, System.EventArgs E)

{

// TextBox1 and textbox2 Are Webform

// controls

Session ["Name"] = textbox1.text;

Session ["email"] = textbox2.text;

Server.Transfer ("ANOTHERWEBFORM.ASPX);

}

Target page code:

PRIVATE VOID PAGE_LOAD

(Object Sender, System.EventArgs E)

{

Label1.text = session ["name"]. ToString (); label2.text = session ["email"]. Tostring ();

Session.Remove ("Name");

Session.Remove ("email");

}

Use Server.Transfer

This method is slightly complicated than the method described above, but it is especially useful in the page value transmission, using the party.

Method, you can access the revealed values ​​in another page, of course, use this method, you need a quota

Wrills some code to create some properties so that you can access it on another page, however, the advantage of this method is also

Obvious. Overall, it is a simple to use this method. Use this method

The process is as follows:

1, add the necessary controls in the page

2. Create a get attribute process for return values

3. Create buttons and link buttons that can return a form

4. Click the event handler to transfer the server.transfer method in the event handler to the specified page

5. In the second page, we can use the context.handler property to get the lead of the previous page instance object.

For use, by it, you can use the value of the control of the previous page.

The following code integrates the code for implementing the above steps:

Source page code:

Add the following code to the page

Public String Name

{

get

{

Return TEXTBOX1.TEXT;

}

}

Public String email

{

get

{

Return TEXTBOX2.TEXT;

}

}

Then call the Server.Transfer method

Private void button1_click

(Object Sender, System.EventArgs E)

{

Server.Transfer ("ANOTHERWEBFORM.ASPX);

}

Target page code:

PRIVATE VOID PAGE_LOAD

(Object Sender, System.EventArgs E)

{

// CREATE Instance of Source Web Form

WebForm1 WF1;

// Get Reference to Current Handler Instance

WF1 = (WebForm1) context.handler;

Label1.text = wf1.name;

Label2.text = wf1.email;

}

Enable a cross-page transfer function in ASP.NET 2.0, its features and usage are in the future!

Page between the page

Way 1:

Plus a line in the HTML code of the received page: <% @ reference page = "Webform1.aspx"%>

WebForm1 fp = (WebForm1) Context.handler;

this.TextBox1.text = fp.name; // name is the first page of public variables

Context provides access to the entire current context (including requested object). You can use the letter between this shared page

interest.

Method 2: GET method

Send a page

Public int sum = 0;

INT i = int.parse (this.textBox1.text) * 2;

Server.Transfer ("Webform2.aspx? Sum =" i);

Receive pages

THISTBOX1.TEXT = Request ["SUM"]. TOSTRING (); or this.TextBox1.text = request.Params ["SUM"]. TOSTRING ();

THIS.TEXTBOX1.TEXT = Request.QueryString ["SUM"];

Method 3: Global variable

Send a page:

Application ["SUM"] = this.TextBox1.Text;

Server.Transfer ("Webform2.aspx");

Receive page:

This.TextBox1.text = (string) Application ["SUM"];

Application is essentially a collection of all files in the entire virtual directory, if you want to use a variable throughout the application

Value, Application object will be the best choice

Use session [""] by session [""]

Method 4:

Send a page:

1. Define static variables: public static string str = ""

2. Str = this.TextBox1.Text;

Server.Transfer ("Webform2.aspx");

Receive page:

1. Introduce the namespace of the first page: use WebApplication1;

2 this.TextBox1.text = Webform1.STR;

转载请注明原文地址:https://www.9cbs.com/read-35370.html

New Post(0)