2011-11-16 118 views
0

我应该改变frmPersonnelVerified后面的代码以获取来自会话状态项的值。更改背后的代码

这里是我的会话状态代码:

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

你可以使用String.Join代替字符串连接 – Guillaume86

回答

3

你存储在会话变量,但随后试图通过Request对象访问它们。将其更改为会话,它应该工作:

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"]; 

    } 

} 

但是,将值放入会话可能会有问题,所以要谨慎。

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"]; 

    } 

} 
0

这是该事件正在处理的顺序:

  1. Page_Load
  2. btnSubmit_Click
  3. Page_Render

如果你的代码摆脱Page_LoadPage_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"]; 
}