ASP.NET Middle Patement Tips

zhaozj2021-02-16  172

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; // Note No space between Name and values, otherwise there is no reference Object of error Response.Redirect (URL); then the key time appears: Place the following code in some events in WebForm2.aspx.cs: lblname.text = request.QueryString ["name"]; ok! Whole pass carry out!

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, find a text control called "TextBox1" in WebForm1 by calling the FinControl () method of the WebForm1 object, which can also be replaced with other controls. No matter which control is found, it must be forced to convert to its type. In this example, it is TEXTBOX, and finally calls its property text, obtains the value in TextBox1, and achieves the effect of the value. This method is also very flexible, can pass multiple page objects, not the transfer value, so the values ​​of certain controls in this page object can be obtained when getting the page object.

(Note: Several methods, in addition to request.form () This type of passage is HTML control, other kinds of Web controls)

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

New Post(0)