How to pass values between ASP. NET web forms pages
Hbzxf (Hao)
Http://blog.csdn.net/hbzxf
ASP. NET web forms provides developers with an excellent event-driven development mode. However, this simple application development mode brings us some small problems. For example, in the traditional ASP application, you can easily transfer one or more values from one page to another through the POST method, and use the same method in ASP. net. Here, we can solve this problem in other ways. ASP. NET provides three methods for us. One is to use querystring to transmit the corresponding value, the other is to transmit the corresponding value through the session variable, and the other is to use the server. the transfer method. The following sections describe one by one:
I. querystring
Querystring is a simple value transfer method. Its disadvantage is that it displays the value to be transferred in the address bar of the browser and cannot pass objects in this method. If you want to pass a security that is not so important or a simple value, it is best to use this method. The following is a small example to complete the data transfer process. The procedure is as follows:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button#click
(Object sender, system. eventargs E)
{
String URL;
Url = "webform2.aspx? Name = "+
Textbox1.text + "& Email =" +
Textbox2.text;
Response. Redirect (URL );
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load
(Object sender, system. eventargs E)
{
Label1.text = request. querystring ["name"];
Label2.text = request. querystring ["email"];
}
Run the command to view the passed result.
Ii. Use session Variables
Using session variables to pass values is the most common method. In this method, values can be transmitted not only to the next page, but also to multiple pages, the variable does not disappear until the value of the session variable is removed. For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button#click
(Object sender, system. eventargs E)
{
Session ["name"] = textbox1.text;
Session ["email"] = textbox2.text;
Response. Redirect ("webform2.aspx ");
}
4. Create a new target page named webform2
5. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
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 ");
}
Run the command to view the passed result.
Iii. Use server. Transfer
Although this method is a bit complicated, it is also a way to pass values on the page.
For example:
1. Create a web form
2. Place a button1 in the new web form, and place two textbox1 and textbox2
3. Create a click event for the button
The Code is as follows:
Private void button#click
(Object sender, system. eventargs E)
{
Server. Transfer ("webform2.aspx ");
}
4. The textbox1 is returned during the creation process. The value code of the textbox2 control is as follows:
Public string name
{
Get
{
Return textbox1.text;
}
}
Public String email
{
Get
{
Return textbox2.text;
}
}
5. Create a new target page named webform2
6. Place label1 and label2 in webform2.
Add the following code to page_load of webform2:
Private void page_load
(Object sender, system. eventargs E)
{
// Create an instance of the original form
Webform1 WF1;
// Obtain the instantiated handle
WF1 = (webform1) Context. Handler;
Label1.text = wf1.name;
Label2.text = wf1.email;
}
Run the command to view the passed result.
The DataGrid application style file defines dynamic styles.
In the DataGrid, select, OK, and delete the multi-row check box list.
ASP. NET controls client properties and events of controls
I often see a similar problem on the Internet: How to Make ASP. NET Server-side controls respond to client events?
ASP. NET Server-side controls can respond to server-side events, so that we can write web pages like writing Windows programs. Sometimes, we do not need to allow the control running on the client to interact with the server. The overhead of this operation is very high: not only the network bandwidth, CPU resources of the server, A PostBack operation will also cause the client browser to "refresh" to affect the interface effect.
If you directly add client Event code to the server-side control in the HTML code of the web form, it is like this:
<Asp: button id = "button1" onclick = "window. Alert ('button pressed ')" text = "button 1"/>
You will find that when you press this button, there is no pre-defined message box, instead, execute the response button in the server code and press the event's button#click method (assuming we have bound this event to the button ). If you view the source code of the client page, there is no self-written code in The onclick event of the button, but it becomes "javascript :__ dopostback (...)".
In fact, this is part of the. NET Framework conversion work. With this "javascript :__ dopostback (...)", you can implement a series of actions such as button submission and server-side response events. (I will describe the script automatically added in this section in detail in future articles .)
If you want the control to respond to events on the client, you must find another method. Fortunately,. Net has provided us with a variety of interfaces to implement these functions.
All server-side controls (button, label, textbox ......) Each has an attribute Attributes -- this is a very important attribute. It is a set of HTML element attributes and events corresponding to server-side controls on the client. With it, we can customize the behavior and appearance of server-side controls on the client.
In addition to the ispostback judgment, we added the following sentence to the page_load event ):
Button1.attributes. Add ("onclick", "window. Alert ('button pressed ')");
After the project is regenerated, browse the page again. This time, we can see that the "button pressed" message box is displayed normally.
Let's go back and explain this code: attributes is the property of button1 and all server-side controls. Add (...) it is a method to which attributes belongs. It is used to add code to the control's HTML tag on the client. The first parameter is the property name, and the second parameter is the property value. Here we add a message box code for The onclick event of the button. View the page source code on the client. You can see that the onclick event of the button contains this code (. net Framework automatically generated "_ dopostback (...) "It follows, and we use add to tell. net Framework: "add our code first! ").
Attributes has another access mode (C # dedicated ):
Button1.attributes ["onclick"] = "window. Alert ('button pressed ')"; // [] indicates the name of the attribute, and the right of the equal sign indicates the value of the attribute.
This method can also be used to change properties for controls using server-side code:
Button1.attributes ["style"] = "fontsize: 9pt"; // you can change the font of a widget to 9 lbs.