Response.Redirect不将文本框传输到另一页上的文本框C#

时间:2018-07-24 20:16:27

标签: c# asp.net session response.redirect

我在尝试将页面frmPersonnel.aspx上每个文本框中的值传输到包含1个文本框的frmPersonnelVerified.aspx页面时遇到问题。我需要通过代码而不是提交按钮的“ PostBackUrl”属性传递文本框值。据我所知,frmPersonnelVerified页面在文本框中有5个返回。因此,似乎有一些值需要传输,但在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"];
    }
}

2 个答案:

答案 0 :(得分:0)

尽管不建议这样做,因为您将文本框值放在会话对象中,您只需在以下的frmPersonnelVerified.aspx中进行检索即可。

for (i = dataMap1.rbegin(); i != dataMap1.rend(); i++) {
    largestNum = i->first;
    j = dataMap2.find(largestNum);
    if (j == dataMap2.end()) { // if largestNum not in dataMap2
        cout << largestNum << endl;
        break;
    }
}

答案 1 :(得分:0)

您可以尝试以下方法:

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");
    }