Response.Redirect 不将文本框转移到另一个页面上的文本框 C#

Response.Redirect Not Transferring Text boxes into Text Box on Another Page C#

我在尝试将第 frmPersonnel.aspx 页上每个文本框中的值传输到包含 1 个文本框的 frmPersonnelVerified.aspx 页面时遇到问题。我需要通过代码而不是提交按钮的 "PostBackUrl" 属性 传递文本框值。据我所知,frmPersonnelVerified 页面的文本框中有 5 returns。因此,似乎有值需要传输,但 frmPersonnelVerified 页面上的文本框中实际上没有列出任何值。

frmPersonnel.aspx

using System;
using`enter code here` System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class frmPersonnel : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        //When the "Cancel" button is selected, the user will be brought back to the home page
        Response.Redirect("frmMain.aspx");
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime StartDate, EndDate;

        StartDate = Convert.ToDateTime(txtStartDate.Text);
        EndDate = Convert.ToDateTime(txtEndDate.Text);

        Panel1.Controls.Add(new LiteralControl("<br />"));
        if (string.IsNullOrWhiteSpace(txtFirstName.Text))
        {
            txtFirstName.BackColor = System.Drawing.Color.Yellow;
            Panel1.Controls.Add(new LiteralControl("<br />"));
            Panel1.Controls.Add(new LiteralControl("<font style= 'color:Red;' > First Name Required!"));
        }
        if (string.IsNullOrWhiteSpace(txtLastName.Text))
        {
            txtLastName.BackColor = System.Drawing.Color.Yellow;
            Panel1.Controls.Add(new LiteralControl("<br />"));
            Panel1.Controls.Add(new LiteralControl("<font style= 'color:Red;' > Last Name Required!"));
        }
        if (string.IsNullOrWhiteSpace(txtPayRate.Text))
        {
            txtPayRate.BackColor = System.Drawing.Color.Yellow;
            Panel1.Controls.Add(new LiteralControl("<br />"));
            Panel1.Controls.Add(new LiteralControl("<font style= 'color:Red;' > Pay Rate Required!"));
        }
        if (string.IsNullOrWhiteSpace(txtStartDate.Text))
        {
            txtPayRate.BackColor = System.Drawing.Color.Yellow;
            Panel1.Controls.Add(new LiteralControl("<br />"));
            Panel1.Controls.Add(new LiteralControl("<font style= 'color:Red;' > Start Date Required!"));
        }
        if (string.IsNullOrWhiteSpace(txtEndDate.Text))
        {
            txtPayRate.BackColor = System.Drawing.Color.Yellow;
            Panel1.Controls.Add(new LiteralControl("<br />"));
            Panel1.Controls.Add(new LiteralControl("<font style= 'color:Red;' > End Date Required!"));
        }
        // verify that the end date is larger than the start date
        if (EndDate < StartDate)
        {
            txtEndDate.BackColor = System.Drawing.Color.Yellow;
            txtStartDate.BackColor = System.Drawing.Color.Yellow;
            Panel1.Controls.Add(new LiteralControl("<br />"));
            Panel1.Controls.Add(new LiteralControl("<font style= 'color:Red;' > Start Date is Greater than End Date!"));
        }
        // If all textboxes are populated, pass the values to the frmPersonnelVerified page
        if (txtFirstName.Text != "" && txtLastName.Text != "" && txtPayRate.Text != "" && txtStartDate.Text != "" && txtEndDate.Text != "")
        {
            Session["txtFirstName"] = txtFirstName.Text;
            Session["txtLastName"] = txtLastName.Text;
            Session["txtPayRate"] = txtPayRate.Text;
            Session["txtStartDate"] = txtStartDate.Text;
            Session["txtEndDate"] = txtEndDate.Text;
            //Need to set session variables for all text boxes
            Response.Redirect("frmPersonnelVerified.aspx");
        }
    }
}

frmPersonnelVerified.aspx

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class frmPersonnelVerified : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Utilize the textbox information to forward to the frmPersonnelVerified page from another page
        txtVerifiedInfo.Text = Request["txtFirstName"] +
            "\n" + Request["txtLastName"] +
            "\n" + Request["txtPayRate"] +
            "\n" + Request["txtStartDate"] +
            "\n" + Request["txtEndDate"];
    }
}

虽然这不是推荐的做法,但由于您将文本框值放在会话对象中,您可以简单地检索 frmPersonnelVerified.aspx

中的以下内容
....

public partial class frmPersonnelVerified : System.Web.UI.Page
{
 protected void Page_Load(object sender, EventArgs e)
 {
    //Utilize the textbox information to forward to the frmPersonnelVerified page from another page
    txtVerifiedInfo.Text = Session["txtFirstName"].ToString() +
        "\n" + Session["txtLastName"].ToString() +
        "\n" + Session["txtPayRate"].ToString() +
        "\n" + Session["txtStartDate"].ToString() +
        "\n" + Session["txtEndDate"].ToString();
 }
}

你可以试试这个:

txtVerifiedInfo.Text = new
StringBuilder(Session["txtFirstName"].ToString())
.Append("\n")
.Append(Session["txtLastName"])
.Append("\n")
.Append(Session["txtPayRate"])
.Append("\n")
.Append(Session["txtStartDate"])
.Append("\n")
.Append(Session["txtEndDate"]).ToString(); 

在您的 frmPersonnel.aspx 中,您也可以这样做:

// If all textboxes are populated, pass the values to the frmPersonnelVerified page
if (!string.IsNullOrEmpty(txtFirstName.Text) && !string.IsNullOrEmpty(txtLastName.Text) && !string.IsNullOrEmpty(txtPayRate.Text) && !string.IsNullOrEmpty(txtStartDate.Text) && !string.IsNullOrEmpty(txtEndDate.Text))
    {
      string delim = "\n";
        Session["txtData"] = txtFirstName.Text + delim + 
                             txtLastName.Text + delim +
                             txtPayRate.Text + delim + 
                             txtStartDate.Text + delim + 
                             txtEndDate.Text; 

        //Need to set session variables for all text boxes
        Response.Redirect("frmPersonnelVerified.aspx");
    }