我所有的验证都不起作用

本文关键字:不起作用 验证 | 更新日期: 2023-09-27 18:15:06

我之前发布过关于我的部分验证不工作。我不得不重做一些代码,现在所有的都不能工作了。

public partial class frmPersonnel : System.Web.UI.Page
{
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            //Checking validation for the text boxes
            bool isValid = true;
            if (string.IsNullOrEmpty((txtFirstName.Text ?? string.Empty).Trim()))
            {
                txtFirstName.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter first name! <br />";
                isValid = false;
            }
            if (string.IsNullOrEmpty((txtLastName.Text ?? string.Empty).Trim()))
            {
                txtLastName.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter last name! <br />";
                isValid = false;
            }
            if (string.IsNullOrEmpty((txtPayRate.Text ?? string.Empty).Trim()))
            {
                txtPayRate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter pay rate! <br />";
                isValid = false;
            }
            if (string.IsNullOrEmpty((txtStartDate.Text ?? string.Empty).Trim()))
            {
                txtStartDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter start date! <br />";
                isValid = false;
            }
            if (string.IsNullOrEmpty((txtEndDate.Text ?? string.Empty).Trim()))
            {
                txtEndDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text += "Please enter end date! <br />";
                isValid = false;
            }
            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
                if (isValid)
            {
                //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)
        {
        }
    }
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmPersonnel.aspx.cs" Inherits="frmPersonnel" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center"> 
    </div>
    <asp:Panel ID="Panel1" runat="server" Height="250px" HorizontalAlign="Left" 
        Width="300px">
        <asp:Label ID="Label1" runat="server" Text="First Name:" Width="80px"></asp:Label>
        <asp:TextBox ID="txtFirstName" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label2" runat="server" Text="Last Name:" Width="80px"></asp:Label>
        <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label3" runat="server" Text="Pay Rate:" Width="80px"></asp:Label>
        <asp:TextBox ID="txtPayRate" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label4" runat="server" Text="Start Date:" Width="80px"></asp:Label>
        <asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="Label5" runat="server" Text="End Date:" Width="80px"></asp:Label>
        <asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="btnSubmit" runat="server" 
            PostBackUrl="~/frmPersonnalVerified.aspx" Text="Submit" 
            onclick="btnSubmit_Click" />
        <asp:Button ID="btnCancel" runat="server" Text="Cancel" />
        <br />
        <asp:Label ID="lblError" runat="server"></asp:Label>
    </asp:Panel>
    </form>
</body>
</html>

我所有的验证都不起作用

注意,您的验证只是设置文本和标签背景。其中没有任何内容显式地告诉应用程序发生了错误,并且必须显示给用户。因此,当代码到达底部并点击Server.Transfer("frmPersonalVerified.aspx");时,它将愉快地转到下一页。在进入下一页之前,您需要某种标志来标记验证是否成功:

        //Checking validation for the text boxes
        bool isValid = true;
        if (string.IsNullOrEmpty((txtFirstName.Text ?? string.Empty).Trim()))
        {
            txtFirstName.BackColor = System.Drawing.Color.Yellow;
            lblError.Text += "Please enter first name! <br />";
            isValid = false;
        }
        if (string.IsNullOrEmpty((txtLastName.Text ?? string.Empty).Trim()))
        {
            txtLastName.BackColor = System.Drawing.Color.Yellow;
            lblError.Text += "Please enter last name! <br />";
            isValid = false;
        }
        // etc.

在进入下一页之前检查isValid:

        if (isValid)
        {
            //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");
        }

我想也许调用Trim()对Null合并的结果可能返回string.Empty可能是问题。

我将在合并之前调用Trim()

当你说"不工作"时,你能澄清一点吗?我想会抛出一个异常,你能告诉我们是哪个异常吗?

编辑:没有看到底部部分!