我应该更改后面的frmPersonnelVerified代码以从Session状态项中获取值。
这是我的会话状态代码:
public partial class frmPersonnel : System.Web.UI.Page
{
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
//Checking validation for the text boxes
if (string.IsNullOrEmpty((txtFirstName.Text ?? string.Empty).Trim()))
{
txtFirstName.BackColor = System.Drawing.Color.Yellow;
lblError.Text += "Please enter first name! <br />";
}
if (string.IsNullOrEmpty((txtLastName.Text ?? string.Empty).Trim()))
{
txtLastName.BackColor = System.Drawing.Color.Yellow;
lblError.Text += "Please enter last name! <br />";
}
if (string.IsNullOrEmpty((txtPayRate.Text ?? string.Empty).Trim()))
{
txtPayRate.BackColor = System.Drawing.Color.Yellow;
lblError.Text += "Please enter pay rate! <br />";
}
if (string.IsNullOrEmpty((txtStartDate.Text ?? string.Empty).Trim()))
{
txtStartDate.BackColor = System.Drawing.Color.Yellow;
lblError.Text += "Please enter start date! <br />";
}
if (string.IsNullOrEmpty((txtEndDate.Text ?? string.Empty).Trim()))
{
txtEndDate.BackColor = System.Drawing.Color.Yellow;
lblError.Text += "Please enter end date! <br />";
}
DateTime dt1;
DateTime dt2;
dt1 = DateTime.Parse(txtStartDate.Text);
dt2 = DateTime.Parse(txtEndDate.Text);
if (DateTime.Compare(dt1, dt2) > 0)
{
//Checking if the end date is greater than the start date
txtStartDate.BackColor = System.Drawing.Color.Yellow;
txtEndDate.BackColor = System.Drawing.Color.Yellow;
lblError.Text += "Start Date must not be greater than End Date! <br />";
}
else
{
//output information if correct validation
Session["txtFirstName"] = txtFirstName.Text;
Session["txtLastName"] = txtLastName.Text;
Session["txtPayRate"] = txtPayRate.Text;
Session["txtStartDate"] = txtStartDate.Text;
Session["txtEndDate"] = txtEndDate.Text;
Server.Transfer("frmPersonalVerified.aspx");
}
}
catch (Exception ex)
{
}
}
}
我有一个提交按钮,当按下该按钮时,如果它正确验证,则应该将上述信息输入到另一页面上的文本框中。现在它不会这样做。
这是关于frmPersonnalVerified的代码:
public partial class frmPersonnelVerified : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Inputs information from frmPersonnel and places it into the
//textbox called "txtVerifiedInfo"
txtVerifiedInfo.Text = Request["txtFirstName"] +
"\n" + Request["txtLastName"] +
"\n" + Request["txtPayRate"] +
"\n" + Request["txtStartDate"] +
"\n" + Request["txtEndDate"];
}
}
答案 0 :(得分:3)
您将变量存储在Session中,然后尝试通过Request对象访问它们。将其更改为Session,它应该可以工作:
public partial class frmPersonnelVerified : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Inputs information from frmPersonnel and places it into the
//textbox called "txtVerifiedInfo"
txtVerifiedInfo.Text = Session["txtFirstName"] +
"\n" + Session["txtLastName"] +
"\n" + Session["txtPayRate"] +
"\n" + Session["txtStartDate"] +
"\n" + Session["txtEndDate"];
}
}
但是,将值放入Session会有问题,所以要小心。
答案 1 :(得分:0)
在经过验证的课程中,您试图从请求对象中获取值而不是会话对象。
Request
对象将允许您访问回发的信息(例如表单字段)或部分查询字符串,并且名称建议与特定请求相关联。 Session
对象与当前用户会话相关联,您可以在该对象中放置和检索任意对象。
作为旁注,因为这似乎与您的问题没有任何关系。 很少需要访问ASP.NET中的Request对象,因为依赖ASP.NET的功能来构建基于请求数据的对象图通常是一种更好的解决方案。
代码中的可能如下所示:
public partial class frmPersonnelVerified : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Inputs information from frmPersonnel and places it into the
//textbox called "txtVerifiedInfo"
txtVerifiedInfo.Text = Session["txtFirstName"] +
"\n" + Session["txtLastName"] +
"\n" + Session["txtPayRate"] +
"\n" + Session["txtStartDate"] +
"\n" + Session["txtEndDate"];
}
}
答案 2 :(得分:0)
这是处理事件的顺序:
Page_Load
btnSubmit_Click
Page_Render
如果您将代码从Page_Load
移至Page_Render
,则应该有效。
protected void Page_Render(object sender, EventArgs e)
{
//Inputs information from frmPersonnel and places it into the
//textbox called "txtVerifiedInfo"
txtVerifiedInfo.Text = Request["txtFirstName"] +
"\n" + Request["txtLastName"] +
"\n" + Request["txtPayRate"] +
"\n" + Request["txtStartDate"] +
"\n" + Request["txtEndDate"];
}