0

Tried searching on ways to transfer more than 1 value, did manage to find a way but when running it, it only transfers one of the values and duplicates it in the second text box although I did put two different values in the textbox. Currently using Microsoft Visual Studio, which are aspx.cs files.

This is the code that is from WebForm1.aspx.cs

protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("WebForm2.aspx?val=" + TextBox1.Text); Response.Redirect("WebForm2.aspx?val=" + TextBox2.Text); } 

This is the code that is supposedly suppose to receive the values from WebForm1

protected void Page_Load(object sender, EventArgs e) { Label1.Text = Request.QueryString["val"]; Label2.Text = Request.QueryString["val"]; } 

After running it, it leads to the design page, entered different values for both textboxes. After clicking the button, it leads to WebForm2.aspx, it shows the value that I had input for TextBox1 in both Label1 and Label2. The value which was in TextBox2 is no where to be found. I'm fairly new to C# coding so I have no idea on where I went wrong.

2
  • &val2=SomethingElse Commented Jun 26, 2017 at 14:30
  • Note that Response.Redirect in ASP.NET WebForms raises a ThreadAbortException after having generated its output, so the second Response.Redirect (or any other code you may have there) is never executed. Commented Jun 26, 2017 at 14:34

1 Answer 1

3

Separate variables in the querystring are separated by an ampersand

WebForm2.aspx?val1=foo&val2=bar

As you see, you'll need differing variables names

Response.Redirect("WebForm2.aspx?val1=" + TextBox1.Text + "&val2=" + TextBox2.Text); 

and

Label1.Text = Request.QueryString["val1"]; Label2.Text = Request.QueryString["val2"]; 
Sign up to request clarification or add additional context in comments.

3 Comments

To enhance this even further, I'd suggest to use Server.UrlEncode(TextBox1.Text) instead of just TextBox1.Text (etc), otherwise non-alphabetical/non-numeric characters in theTextboxes may cause the URL to malfunction or misbehave.
@PeterB not to mention using Server.HtmlEncode before adding it to a label to avoid XSS attacks.
Yup, indeed. We should try pair programming one day :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.