lundi 25 janvier 2016

New line a single label used in multiple if statements

I'm trying to find out is there a way the to create a new line i a single label used in 5 different if statements in C# ASP.net. The label gives an error message for each textbox not filled in, I'm trying to place all error messages on the own separate lines. Here's my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class frmPersonnel : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void btnSubmit_Click(object sender, EventArgs e)
  {
    bool txtFieldError = false;
    lblError.Text = "";
    txtFirstName.BackColor = System.Drawing.Color.White;
    txtLastName.BackColor = System.Drawing.Color.White;
    txtPayRate.BackColor = System.Drawing.Color.White;
    txtStartDate.BackColor = System.Drawing.Color.White;
    txtEndDate.BackColor = System.Drawing.Color.White;

    if (txtFirstName.Text.Trim() == "")
    {
        txtFieldError = true;
        lblError.Text = lblError.Text + "Must enter first name";
        txtFirstName.BackColor= System.Drawing.Color.Yellow;            
    }

    if (txtLastName.Text.Trim() == "")
    {
        txtFieldError = true;
        lblError.Text = lblError.Text + "Must enter Last name";
        txtLastName.BackColor = System.Drawing.Color.Yellow;
    }

    if (txtPayRate.Text.Trim() == "")
    {
        txtFieldError = true;
        lblError.Text = lblError.Text + "Must enter pay rate";
        txtPayRate.BackColor = System.Drawing.Color.Yellow;
    }

    if (txtStartDate.Text.Trim() == "")
    {
        txtFieldError = true;
        lblError.Text = lblError.Text + "Must enter start date";
        txtStartDate.BackColor = System.Drawing.Color.Yellow;
    }

    if (txtEndDate.Text.Trim() == "")
    {
        txtFieldError = true;
        lblError.Text = lblError.Text + "Must enter end date";
        txtEndDate.BackColor = System.Drawing.Color.Yellow;
    }

    if (!txtFieldError)
    {
        DateTime strDate;
        DateTime nDate;

        strDate = DateTime.Parse(txtStartDate.Text);
        nDate = DateTime.Parse(txtEndDate.Text);

          if (DateTime.Compare(strDate, nDate) >= 0)
          {
           txtFieldError = true;
           lblError.Text = lblError.Text + "Must enter start date earlier than end date";
           txtStartDate.BackColor = System.Drawing.Color.Yellow;
           txtEndDate.BackColor = System.Drawing.Color.Yellow;
          }
    }

    if (!txtFieldError)
    {
        Session["txtFirstName"] = txtFirstName.Text;
        Session["txtLastName"] = txtLastName.Text;
        Session["txtPayRate"] = txtPayRate.Text;
        Session["txtStartDate"] = txtStartDate.Text;
        Session["txtEndDate"] = txtEndDate.Text;

        Response.Redirect("frmPersonnelVerified.aspx");
    }   

  }
}

If I leave all fields blank I get all the error messages in a continuous line and wraps to the next line. I want each error to have it's own line. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire