ASP.NET Middle Patement Tips

xiaoxiao2021-03-06  150

There are several page-based transmission methods in ASP.NET: common QueryString. This method is the easiest, but the disadvantage is that it is displayed in the address bar to be transmitted, if it is in security Information is not a good solution. Another disadvantage is that it cannot pass objects. This method is suitable for transmitting a simple value and unimportant information. example:

There are two pages: Webform1.aspx, WebForm2.aspx.

Place the following code in some events of WebForm1.aspx.cs:

String url = "Webform2.aspx? Name =" this.txtname.text;

Response.Redirect (URL);

Then there is a key moment: Some events are placed in some events in WebForm2.aspx.cs:

LBLName.Text = Request.QueryString ["name"];

OK! The entire pass value is complete!

Another is to use the session variable to pass the value, which is also commonly used. With the session, it is more flexible and can be transmitted between multiple pages. After the REMOVE is called, the session is invalid. Also use two pages: WebForm1.aspx.cs is written in the following code: session ["name"] = txtname.text; response.redirect ("Webform2.aspx"); below Webform1.aspx.cs Remove the value of the session: lblname.text = session ["name"]. ToString (); // Because it is an object, you must convert the type session.remove ("name") ;? // makes the session.

The third way is to use the Request object to get the value, (Note: HTML control here) This is relatively simple. The implementation is as follows:

In WebForm1.aspx:

Name:

?

Writing in WebForm2.aspx.cs: Response.write (Request.form ["txtname"]);

When you click the Submit button, the value is taken by Request.form ["txtname"].

The fourth method is to transmit the value to the transfer () method of the Server object, which receives a page object.

Let's take a look at this implementation:

Write the following code in an event of WebForm1.aspx: Server.Transfer ("WebForm2.aspx"); // Pass the page object next to get webform1 page objects in WebForm2.aspx.cs. If (context.handler is webform1) // determines whether it is transferred WebForm1, because there may be multiple delivery objects. {WebForm1 F1 = (WebForm1) Context.Handler; // Create a webform1 page object through context.handler, returns an Object Response.write ("Hey, I Get It With Context Handler ((TextBox) F1.FindControl ("TextBox1"))). TEXT "

"); ??} // Finally, by calling the FinControl () method of the WebForm1 object to find a text control named" TextBox1 "in WebForm1, you can also replace it with other controls, no matter which control is found. It must be forced to convert it to its type. In this example, it is TEXTBOX, and finally calls its property text, get the value of the TEXTBOX1, to achieve the effect of the value. This method is equally flexible, you can pass multiple page objects, Instead of transmission values, you can get the values ​​of certain controls in this page object when you get the page object. (Note: The above methods, in addition to request.form () this transmission method Outside the HTML control, several other types are used

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

New Post(0)