I'm trying to write C# code for a code-behind page in an ASP.project.
I'm to the point where if any of the fields are blank, the code returns the page with the missing fields highlighted in yellow, and if all the fields are filled, then the page saves session state and opens another page via a Response.Redirect.
I'd like to verify the txtPayRate.Text as a number, and verify and format the two date fields as well, but I'm not sure what the best way to do that is in an ASP project. In a Windows form, I'd TryParse the numbers to see if they would parse into double precision integers, or do a quick and dirty convert with a try/catch block. Working with server/client is new to me. Is there any best practice way to do number or date validation server-side in ASP?
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class frmPersonnel : System.Web.UI.Page
{
private string _firstName;
private string _lastName;
private double _payRate;
private string _startDate;
private string _endDate;
bool _frmPersonnelIsValid = true;
protected void btnCancel_Click(object sender, EventArgs e)
{
// Redirect to Main page on Cancel button click
Response.Redirect("frmMain.aspx");
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (txtFirstName.Text.Trim() != "")
{
txtFirstName.BackColor = System.Drawing.Color.White;
_firstName = txtFirstName.Text.Trim();
}
else
{
_frmPersonnelIsValid = false;
txtFirstName.BackColor = System.Drawing.Color.Yellow;
}
if (txtLastName.Text.Trim() != "")
{
txtLastName.BackColor = System.Drawing.Color.White;
_lastName = txtLastName.Text.Trim();
}
else
{
_frmPersonnelIsValid = false;
txtLastName.BackColor = System.Drawing.Color.Yellow;
}
if (txtPayRate.Text.Trim() != "")
{
txtPayRate.BackColor = System.Drawing.Color.White;
}
else
{
_frmPersonnelIsValid = false;
txtPayRate.BackColor = System.Drawing.Color.Yellow;
}
if (txtStartDate.Text.Trim() != "")
{
txtStartDate.BackColor = System.Drawing.Color.White;
_startDate = txtStartDate.Text.Trim();
}
else
{
_frmPersonnelIsValid = false;
txtStartDate.BackColor = System.Drawing.Color.Yellow;
}
if (txtEndDate.Text.Trim() != "")
{
txtEndDate.BackColor = System.Drawing.Color.White;
_endDate = txtEndDate.Text.Trim();
}
else
{
_frmPersonnelIsValid = false;
txtEndDate.BackColor = System.Drawing.Color.Yellow;
}
if (_frmPersonnelIsValid != true) return;
Session["txtFirstName"] = txtFirstName.Text.Trim();
Session["txtLastName"] = txtLastName.Text.Trim();
Session["txtPayRate"] = txtPayRate.Text.Trim();
Session["txtStartDate"] = txtStartDate.Text.Trim();
Session["txtEndDate"] = txtEndDate.Text.Trim();
Response.Redirect("frmPersonnelVerified.aspx");
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["txtFirstName"] != null)
{
txtFirstName.Text = Session["txtFirstName"].ToString();
}
if (Session["txtLastName"] != null)
{
txtLastName.Text = Session["txtLastName"].ToString();
}
if (Session["txtPayRate"] != null)
{
txtPayRate.Text = Session["txtPayRate"].ToString();
}
if (Session["txtStartDate"] != null)
{
txtStartDate.Text = Session["txtStartDate"].ToString();
}
if (Session["txtEndDate"] != null)
{
txtEndDate.Text = Session["txtEndDate"].ToString();
}
}
}