Mike
Mike

Reputation: 2313

Changing the code behind

I'm supposed to change the frmPersonnelVerified code behind to get the values from the Session state items.

Here is my session state code:

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) 
        { 

        } 
    } 
}

I have a submit button that when pressed is supposed to input the above information into a text box on another page if it validates correctly. Right now it doesn't do that.

Here is my code on 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"]; 

    } 

}

Upvotes: 0

Views: 1155

Answers (3)

Rune FS
Rune FS

Reputation: 21742

In your verified class you're trying to get the values from the request object not the session object.

The Request object will give you accesss to information either posted back (E.g. form fields) or part of the query string and as the name suggest is associated with a specific request. The Session object is associated with the current user session and you can place and retrieve arbitrary objects in that object.

As a side note since this does not seem to have anything to do with your problem. It's seldom needed to access the Request object in ASP.NET for values relying on ASP.NETs capabilities to construct object graphs based on request data is generally a better solution.

in code this could look like:

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

    } 

}

Upvotes: 0

Garrett Vlieger
Garrett Vlieger

Reputation: 9494

You're storing the variables in Session but then trying to access them through the Request object. Change it to Session, and it should work:

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

    } 

}

However, putting values into Session can be problematic so be cautious.

Upvotes: 3

Dennis Traub
Dennis Traub

Reputation: 51624

This is the order in which the events are being handled:

  1. Page_Load
  2. btnSubmit_Click
  3. Page_Render

If you move your code from Page_Load to Page_Render it should work.

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

Upvotes: 0

Related Questions