【发布时间】:2018-07-24 20:16:26
【问题描述】:
我在尝试将页面 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"];
}
}
【问题讨论】:
-
我认为你需要回到关于
GET和POST的基础知识——即使 ASP.net WebForms 将它们从你身上抽象出来(尽管 Web 表单的核心功能在POSTbacks 中) .快速查看您的代码显示您将数据存储在Session中,但您希望它们在Request中?您可以通过GET(而不是POST)提交表单,但那是not what your code is doing。
标签: c# asp.net session response.redirect