如何进行简单的基本数据验证

时间:2012-01-27 19:19:44

标签: c# silverlight

我有一个小型网页表单,用户将输入一些患者数据,然后将其传递给保险中心。我只是想提供一些基本的验证,例如社会安全号码sb数字,10分之一,年龄sb numercial和可能超过一定年龄,等等。我怎么能这样做,在这个代码中?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
...


namespace PBM
{
    public partial class MainPage : UserControl
    {
        private DomainServicePBM _context = new DomainServicePBM();
        public MainPage()
        {
            InitializeComponent();
            this.ObjPatient = new Patient();
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            dgPatient.ItemsSource = _context.Patients;
            _context.Load(_context.GetPatientsBySearchQuery(sTxtFirstName.Text.Trim(), sTxtLastName.Text.Trim(), sCombGender.SelectedIndex == 1 ? false: sCombGender.SelectedIndex == 2 ? true : new bool?()));

            PagedCollectionView itemListView = new PagedCollectionView(dgPatient.ItemsSource);
            dpPatient.Source = itemListView;
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            //_context = new DomainServicePBM();
            if (ObjPatient != null && ObjPatient.PatientID > 0)
            {
                Patient p = _context.Patients.Single(pat => pat.PatientID == this.ObjPatient.PatientID);
                p.FirstName = txtFirstName.Text.Trim();
                p.LastName = txtLastName.Text.Trim();
                p.MiddleName = txtMiddleName.Text.Trim();
                p.Gender = cmbGender.SelectedIndex == 0 ? false : true;
                p.DOB = ctrlDTDOB.SelectedDate;
                p.Age = Convert.ToInt32(txtAge.Text.Trim());
                p.MaterialStatus = cmbMaritalStatus.SelectedIndex == 0 ? (byte)MaritalStatus.Single :
                    cmbMaritalStatus.SelectedIndex == 1 ? (byte)MaritalStatus.Married : (byte)MaritalStatus.Divorced;
                p.SSN = txtSSN.Text.Trim();
                p.MedicalID = txtMedicalID.Text.Trim();
                p.Medicare = txtMedicare.Text.Trim();
                p.Race = txtRace.Text.Trim();
                p.AdmitFrom = ctrlDTAdmitFrom.SelectedDate;
            }
            else
            {
                _context.Patients.Add(new Patient
                {
                    FirstName = txtFirstName.Text.Trim(),
                    LastName = txtLastName.Text.Trim(),
                    MiddleName = txtMiddleName.Text.Trim(),
                    Gender = cmbGender.SelectedIndex == 0 ? false : true,
                    DOB = ctrlDTDOB.SelectedDate,
                    Age = Convert.ToInt32(txtAge.Text.Trim()),
                    MaterialStatus = cmbMaritalStatus.SelectedIndex == 0 ? (byte)MaritalStatus.Single :
                    cmbMaritalStatus.SelectedIndex == 1 ? (byte)MaritalStatus.Married : (byte)MaritalStatus.Divorced,
                    SSN = txtSSN.Text.Trim(),
                    MedicalID = txtMedicalID.Text.Trim(),
                    Medicare = txtMedicare.Text.Trim(),
                    Race = txtRace.Text.Trim(),
                    AdmitFrom = ctrlDTAdmitFrom.SelectedDate
                });
            }
            _context.SubmitChanges(
                (SubmitOperation so) =>
                {
                    if (so.HasError)
                    {
                        MessageBox.Show("Patient Info Not Saved.");
                    }
                    else
                    {
                        MessageBox.Show("Patient Info Saved Successfully.");
                        ResetControls();
                        this.ObjPatient = new Patient();
                    }
                }, null);


        }



        Patient ObjPatient { get; set; }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            this.ObjPatient = new Patient();
        }

        private void ResetControls()
        {
            txtFirstName.Text = txtLastName.Text = txtMiddleName.Text = txtMedicalID.Text = txtMedicare.Text = txtRace.Text = txtSSN.Text = txtAge.Text = string.Empty;
            cmbGender.SelectedIndex = cmbMaritalStatus.SelectedIndex = 0;
            ctrlDTAdmitFrom.SelectedDate = ctrlDTDOB.SelectedDate = DateTime.Now;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            this.ObjPatient = dgPatient.SelectedItem as Patient;
            if (this.ObjPatient != null)
            {
                _context.Patients.Remove(this.ObjPatient);
                _context.SubmitChanges(
                    (SubmitOperation so) =>
                    {
                        if (so.HasError)
                        {
                            MessageBox.Show(so.Error.ToString());
                        }
                        else
                        {
                            MessageBox.Show("Patient Deleted Successfully.");
                        }

                    }, null);
            }
            else
            {
                MessageBox.Show("Please select patient first.");
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            this.ObjPatient = dgPatient.SelectedItem as Patient;
            if (ObjPatient != null)
            {
                txtFirstName.Text = ObjPatient.FirstName;
                txtLastName.Text = ObjPatient.LastName;
                txtMiddleName.Text = ObjPatient.MiddleName;
                cmbGender.SelectedIndex = ObjPatient.Gender == true ? 0 : 1;
                cmbMaritalStatus.SelectedIndex = ObjPatient.MaterialStatus == 1 ? 0 : ObjPatient.MaterialStatus == 2 ? 1 : 2;
                txtAge.Text = Convert.ToString(ObjPatient.Age);
                ctrlDTDOB.SelectedDate = ObjPatient.DOB;
                ctrlDTAdmitFrom.SelectedDate = Convert.ToDateTime(ObjPatient.AdmitFrom);
                txtMedicalID.Text = ObjPatient.MedicalID;
                txtMedicare.Text = ObjPatient.Medicare;
                txtRace.Text = ObjPatient.Race;
                txtSSN.Text = ObjPatient.SSN;
            }
        }

        private void dpPatient_PageIndexChanged(object sender, EventArgs e)
        {
            _context.Patients.Skip(dpPatient.PageIndex).Take(1);
        }

    }

    public enum MaritalStatus {
        Single=0,
        Married=1, 
        Divorced = 2
    }
}

1 个答案:

答案 0 :(得分:0)

我会看看DataAnnations命名空间。它提供了用于定义各种验证的属性。您需要将这些属性应用于需要验证的属性。然后,您需要一种方法来检查这些属性是否通过验证。这通常使用反射来完成。看一下这个answer