Asp. NET page transfer value of several ways "summary collection" __.net

Source: Internet
Author: User

. NET page transfer values in several ways "Reprint + Collection"

************************************************

First, the use of QueryString

QueryString is a very simple way of passing values, the disadvantage of which is to display the value to be transferred in the browser's address bar, and in this method can not pass the object. It's best to use this method if you want to pass a security that is not so important or a simple value. Here is a small example to complete the work of the transfer, the following steps:
1. Create a Web Form
2. Place a button1 in the new Web form, placing two Textbox1,textbox2
3. Create Click events for button buttons
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, a new target page named WebForm2
5. Place two Label1,label2 in WebForm2
Add the following code to the Page_Load of WebForm2:
private void Page_Load (Object Sender,system.eventargs e)
{
label1.text=request.querystring["Name"];
label2.text=request.querystring["Email"];
}
Run, you can see the results after the delivery.

************************************************

Ii. use of Application object variables
The scope of the Application object is the entire global, which means it works for all users. Its commonly used methods are lock and unlock.
A.aspx's C # code
private void Button1_Click (object sender, System.EventArgs e)
{
application["name"] = Label1.Text;
Server.Transfer ("b.aspx");
}

C # code in b.aspx
private void Page_Load (object sender, EventArgs e)
{
String name;
Application.Lock ();
Name = application["Name"]. ToString ();
Application.UnLock ();
}

************************************************

Third, the use of Session variables

The most common way to use the session variable is to pass the value to the next page and cross over to multiple pages until the value of the session variable is removed, and the variable disappears. For example, take a look at:
1. Create a Web Form
2. Place a button1 in the new Web form, placing two Textbox1,textbox2
3. Create Click events for button buttons
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, a new target page named WebForm2
5. Place two Label1,label2 in WebForm2
Add the following code to the 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, you can see the results after the delivery.

*************************************

Four. Use Cookie object variables
This is also common use of the method, as with the session, what it is for each user, but there is an essential difference, that is, cookies are stored in the client, and the session is stored on the server side. And the use of cookies should be used in conjunction with the ASP.net built-in object request.

A.aspx's C # code
private void Button1_Click (object sender, System.EventArgs e)
{
HttpCookie cookie_name = new HttpCookie ("name");
Cookie_name. Value = Label1.Text;
Reponse.appendcookie (Cookie_name);
Server.Transfer ("b.aspx");
}

C # code in b.aspx
private void Page_Load (object sender, EventArgs e)
{
String name;
Name = request.cookie["Name"]. Value.tostring ();
}

************************************************

V. Use of Server.Transfer

Although this approach is a bit complicated, it's also a way to pass a value on a page.
For example, take a look at:
1. Create a Web Form
2. Place a button1 in the new Web form, placing two Textbox1,textbox2
3. Create Click events for button buttons
The code is as follows:
private void (object Sender,system.eventargs e)
{
Server.Transfer ("webform2.aspx");
}
4. Create a procedure in the Web form to return the value code of the Textbox1,textbox2 control as follows:
public string Name
{
get {return textbox1.text;}
}
public string Email
{
Get{return TextBox2.Text;}
}
5, a new target page named WebForm2
6. Place two Label1,label2 in WebForm2
Add the following code to the Page_Load of WebForm2:
private void Page_Load (Object Sender,system.eventargs e)
{
To create an instance of WebForm
WebForm1 WF1;
Get the instantiated handle
wf1= (WebForm1) Context.Handler;
Label1.text=wf1. Name;
Label2.text=wf1. Email;
}

Summed up is QueryString is quite simple, the session is relatively complex; QueryString security is poor, session security is high; QueryString can achieve simple data delivery for each Web page, usually the string type, Session enables the delivery of complex types of data between Web pages, and so on.

************************************************************

************************************************************
<<< 2 >>>

Replytopic.aspx? forum_id=5&id=256434&ppage=1&quote=256434
The name of the page? The variable name to be passed (take it yourself) = variable & the variable name to pass (take yourself) = variable & the variable name to pass (take it yourself) = variable


Accept the page and write request.querystring["variable name to pass"].

3.datagrid How to pass multiple values to another page
<asp:templatecolumn headertext= "Serial No." Headerstyle-width = "9%" >
<itemtemplate>
<asp:hyperlink ID = "link_serial" Runat =server target=_blank text= ' <%# container.dataitem ("Un_seri Al_no ")%> ' navigateurl= ' <%#". /hotline/receive_pavilion.aspx?model_no= "+ Trim (DataBinder.Eval (Container.DataItem," Un_model_no ")) +" &serial _no= "+trim (DataBinder.Eval (Container.DataItem," Un_serial_no ")) +" &cu_id= "+trim (DataBinder.Eval ( Container.DataItem, "un_customer_id"))%> ' >
</asp:hyperlink>
</itemtemplate>
</asp:templatecolumn>
</asp:templatecolumn>

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.