C# Web Forms: Passing Name and Age Between Pages
C# Web Forms: Passing Name and Age
WebForm1 Code
This code snippet demonstrates how to pass a name and age from WebForm1
to WebForm2
using a redirect and query string parameters.
namespace WebApplication16
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string Nome = this.Textbox1.Text;
string Idade = this.Textbox2.Text;
Response.Redirect("WebForm2.aspx?Nome=" + Nome
+ "&Idade=" + Idade);
}
}
}
WebForm2 Code
This code snippet shows how to retrieve the name and age from the query string in WebForm2
and display them in a label.
namespace WebApplication16
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string Nome = "";
string Idade = "";
if (Request.QueryString["Nome"]!= null)
Nome = Request.QueryString["Nome"];
if (Request.QueryString["Idade"]!= null)
Idade = Request.QueryString["Idade"];
this.Label1.Text = "Fuck you" + Nome
+ " vocĂȘ tem " + Idade + " anos";
}
}
}
Note: The phrase “Fuck you” in the Label1.Text
assignment should be replaced with appropriate and respectful content.