简体   繁体   中英

Posting User Input from one page to another

I'm creating an ASP.NET webpage that allows for personnel information to be entered into textboxes (firstName, lastName, payRate, startDate, endDate). Once entered and the "submit" button is clicked, a couple of validation rules are checked and if all fields are validated correctly, then the information is passed to second page to be displayed (again inside a multi-line textbox).

Now the problem that I am encountering is that when the submit button is pushed, the text box on the second page doesn't display the information entered; instead it displays the following:

"System.Web.UI.WebControls.TextBox" <-- this is displayed five times (once for each text field)

Below is an exempt from the submit button event handler from the first page and the page load handler from the verified information page.

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string Msg = "";
    Boolean validatedState = true;

    //validating first name text box
    if (Request["txtFirstName"].ToString().Trim() == "")
    {
        txtFirstName.BackColor = System.Drawing.Color.Yellow;
        Msg = "Please enter first name";
        lblError.Text += Msg;
        validatedState = false;
    }
    else
    {
        txtFirstName.BackColor = System.Drawing.Color.White;
        Msg = "";
        lblError.Text = "";
        validatedState = true;
    }

These if statements are repeated for all five fields and then the information is stored in session state and transfer to the next page like so:

if (validatedState)
    {
        Session.Add("txtFirstName", txtFirstName);
        Session.Add("txtLastName", txtLastName);
        Session.Add("txtPayRate", txtPayRate);
        Session.Add("txtStartDate", txtStartDate);
        Session.Add("txtEndDate", txtEndDate);
        Server.Transfer("frmPersonnelVerified.aspx");
    }

Below is the page load handler from the verified information page that is meant to retrieve and display the information from session object.

protected void Page_Load(object sender, EventArgs e)
{
    //requesting information from personnel form; displaying in multi-line textbox called txtVerifiedInfo
    txtVerifiedInfo.Text = Session["txtFirstName"].ToString() +
                           Session["txtLastName"].ToString() +
                           Session["txtPayRate"].ToString() +
                           Session["txtStartDate"].ToString() +
                           Session["txtEndDate"];
}

as Khan said:

    Session.Add("txtFirstName", txtFirstName);

should be

    Session.Add("txtFirstName", txtFirstName.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM