Marcus Marcus - 1 month ago 15
C# Question

Response redirect not properly sending a variable to second form

I am using Response.Redirect to send information to a second form, most of which is text captured in an input field.

the code for sending those is simple enough.

Response.Redirect("secondwebform.aspx?Adress=" + adress.Value);


while the variable is as follows

Response.Redirect("secondwebform.aspx?StringToSend");


in my second webform.aspx.cs I have the code that catches the data on pageload and enters it into an input field automatically.

adress.Value = Request.QueryString["Adress"];


problem comes when trying to use the string variable and enter it in an input field or textbox as well.

my code for that is as follows:

string StringToUse = Request.QueryString["StringToSend"];
TextBox1.Text = StringtoUse;


I researched questions for this case and used the code for correct answers but in my situation the string variable does not work.

Win Win
Answer

We normally user QueryString to pass ID or very small data.

You do not want to pass address in Query String, because it might contain special characters and you will need up with invalid URL.

For your scenario, you want to use SessionState. For example,

First Form

<%@ 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>

Second Form

<%@ 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>
Comments