C# 响应重定向未正确地将变量发送到第二个表单
我使用Response.Redirect将信息发送到第二个表单,其中大部分是在输入字段中捕获的文本 发送这些信息的代码非常简单C# 响应重定向未正确地将变量发送到第二个表单,c#,string,response.redirect,C#,String,Response.redirect,我使用Response.Redirect将信息发送到第二个表单,其中大部分是在输入字段中捕获的文本 发送这些信息的代码非常简单 Response.Redirect("secondwebform.aspx?Adress=" + adress.Value); 而变量如下 Response.Redirect("secondwebform.aspx?StringToSend"); 在我的第二个webform.aspx.cs中,我有一个代码,可以捕获pageload上的数据并将其自动输入到输入字段中
Response.Redirect("secondwebform.aspx?Adress=" + adress.Value);
而变量如下
Response.Redirect("secondwebform.aspx?StringToSend");
在我的第二个webform.aspx.cs中,我有一个代码,可以捕获pageload上的数据并将其自动输入到输入字段中
adress.Value = Request.QueryString["Adress"];
当尝试使用字符串变量并将其输入到输入字段或文本框中时,会出现问题
我的代码如下:
string StringToUse = Request.QueryString["StringToSend"];
TextBox1.Text = StringtoUse;
我研究了本例中的问题,并使用代码获得正确答案,但在我的情况下,字符串变量不起作用。我们通常使用QueryString传递ID或非常小的数据 您不希望在查询字符串中传递地址,因为它可能包含特殊字符,并且您需要提供无效的URL 对于您的场景,您希望使用SessionState。比如说, 第一种形式
受保护的无效转发按钮\u单击(对象发送方,事件参数e)
{
会话[“Name”]=NameTextBox.Text;
Response.Redirect(“~/SecondForm.aspx”);
}
第一种形式
第二种形式
受保护的无效页面加载(对象发送方、事件参数e)
{
namelab.Text=会话[“Name”]作为字符串;
}
第二种形式
URLsecondwebform.aspx?StringToSend
没有为查询参数StringToSend
赋值,因此请求。QueryString[“StringToSend”]
将为空字符串。您打算将其设置为值吗?我打算以第二种形式接收相同的字符串,并用其字符填充文本框。此外,我的字符串实际上包含字符。
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="FirstForm.aspx.cs" Inherits="DemoWebForm.FirstForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script runat="server">
protected void ForwardButton_Click(object sender, EventArgs e)
{
Session["Name"] = NameTextBox.Text;
Response.Redirect("~/SecondForm.aspx");
}
</script>
<form id="form1" runat="server">
<h1>First Form</h1>
<asp:TextBox runat="server" ID="NameTextBox" />
<asp:Button ID="ForwardButton" runat="server"
OnClick="ForwardButton_Click" Text="Forward Name to Second Form" />
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true"
CodeBehind="SecondForm.aspx.cs" Inherits="DemoWebForm.SecondForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
NameLabel.Text = Session["Name"] as string;
}
</script>
<form id="form1" runat="server">
<h1>Second Form</h1>
<asp:Label runat="server" ID="NameLabel" />
</form>
</body>
</html>