我试图找出是否有办法在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");
}
}
}
如果我将所有字段留空,我会以连续的行显示所有错误消息并将其包装到下一行。我希望每个错误都有它自己的行。提前致谢!
答案 0 :(得分:0)
在这种情况下,您最好的选择是StringBuilder
来构建错误消息,最后在标签中显示错误。你可以通过以下方式实现这一目标:
StringBuilder ErrorBuilder = new StringBuilder();
ErrorBuilder.Append("Following are errors :");
if (txtFirstName.Text.Trim() == "")
{
txtFieldError = true;
ErrorBuilder.AppendLine("Must enter first name");
txtFirstName.BackColor = System.Drawing.Color.Yellow;
}
if (txtLastName.Text.Trim() == "")
{
txtFieldError = true;
ErrorBuilder.AppendLine("Must enter Last name");
txtLastName.BackColor = System.Drawing.Color.Yellow;
}
// Rest of conditions here
lblError.Text = ErrorBuilder.ToString();
在我们使用+=
时,为了best option is stringBuilder而不是ErrorBuilder.AppendLine
连接字符串,newLine字符会自动附加字符串。
答案 1 :(得分:0)
您应该能够将HTML放在那里,例如:
lblError.Text = lblError.Text + "<br> Must enter end date";