简体   繁体   English

C#-在ASPX页面之间传输多个值

[英]C# - transfer more than one value between aspx pages

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. 当前使用Microsoft Visual Studio,它们是aspx.cs文件。

This is the code that is from WebForm1.aspx.cs 这是来自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 这是假定应该从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. 单击按钮后,它会指向WebForm2.aspx,它显示了我在Label1和Label2中为TextBox1输入的值。 The value which was in TextBox2 is no where to be found. TextBox2中的值在哪里都找不到。 I'm fairly new to C# coding so I have no idea on where I went wrong. 我是C#编码的新手,所以我不知道哪里出错了。

Separate variables in the querystring are separated by an ampersand 查询字符串中的单独变量由与号分隔

WebForm2.aspx?val1=foo&val2=bar 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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM