【发布时间】:2016-01-25 12:32:51
【问题描述】:
我正在尝试找出在 C# ASP.net 中的 5 种不同 if 语句中使用的单个标签创建新行的方法。标签为每个未填写的文本框提供错误消息,我试图将所有错误消息放在自己的单独行上。这是我的代码:
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");
}
}
}
如果我将所有字段留空,我会在一个连续的行中收到所有错误消息并换行到下一行。我希望每个错误都有自己的行。提前致谢!
【问题讨论】:
标签: c# asp.net if-statement label newline