我之前发布了部分验证不起作用。我不得不重做一些代码,现在所有代码都无效。
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>
答案 0 :(得分:3)
请注意,您的验证只是设置文字和标签背景。没有任何内容明确告诉应用程序发生了错误,必须向用户显示。因此,当代码到达底部并点击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");
}
答案 1 :(得分:1)
我猜想,或许可以返回Trim()
的Null coalesce结果调用string.Empty
可能是问题。
在进行合并之前,我会在字符串上调用Trim()
。
当你说“不工作”时,你能澄清一下吗?我想会抛出异常,你能告诉我们哪一个吗?
编辑:没看到底部!