2011-11-16 120 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"]; 
}