ASP.NET WEB FORMS provides developers with an excellent event-driven development model. However, this simple application development model has brought us some small problems. For example, in a traditional ASP application, you can easily transfer a value or multiple values from a page through the POST method. To another page (request()/request.form()/request.querystring()), it is a bit troublesome to implement it in ASP.NET using the same method. Here are other ways to resolve this situation. ASP.NET provides us with three ways, 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 implement it through the Server.Transfer method.
1. Use Querystring
Querystring is a very simple way to pass values. Its disadvantage is that the value to be transmitted will be displayed in the browser's address bar, and objects cannot be passed in this method. This method is best used if you want to pass a value that is not too important or a simple value. Let's use a small example to complete the value transfer work. The steps are as follows:
1. Create a web form
2. Place a button1 in the new web form and two TextBox1 and TextBox2
3. Create the click event code for the button button 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 and name it webform2
5. Place two Label1 and Label2 in webform2
Add the following code in Page_Load of webform2:
private void Page_Load(object sender,System.EventArgs e)
{
Label1.Text=Request.QueryString["name"];
Label2.Text=Request.QueryString["Email"];
}
Run it and you can see the results after the transfer.
2. Use Session variables. Using Session variables to pass values is the most common way. This method can not only pass the value to the next page, but also cross-pass it to multiple pages until the value of the Session variable is removed. The variable will disappear. Take an example:
1. Create a web form
2. Place a button1 in the new web form and two TextBox1 and TextBox2
3. Create the click event code for the button button 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 and name it webform2
5. Place two Label1 and Label2 in webform2
Add the following code in 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 it and you can see the results after the transfer.
3. Use Server.Transfer
Although this method is a bit complicated, it is still a way to pass values on the page.
Take an example:
1. Create a web form
2. Place a button1 in the new web form and two TextBox1 and TextBox2
3. Create the click event code for the button button as follows:
private void (object sender,System.EventArgs e)
{
Server.Transfer("webform2.aspx");
}
4. Create a process to return the value codes of TextBox1 and TextBox2 controls 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 two Label1 and Label2 in webform2
Add the following code in Page_Load of webform2:
private void Page_load(object sender,System.EventArgs e)
{
//Create an instance of webform
webform1 wf1;
//Get the instantiated handle
wf1=(webform1)Context.Handler;
Label1.Text=wf1.Name;
Label2.Text=wf1.Email;
}
These three methods are common.