为表单上的各种错误添加到消息中

本文关键字:添加 消息 错误 表单 | 更新日期: 2023-09-27 17:57:39

我正在尝试制作一个表单,如果没有输入信息,则会根据没有输入的信息得到一系列错误消息。不幸的是,我遇到了一个问题,保存消息的变量Msg不起作用(我认为这很有意义)。它确实显示了错误,但以段落形式显示,而不是以列表形式显示。

如何将所有错误编译为带有换行符的消息?

我试着把"''n"answers"''r''n"包括在内,但没有用。

我现在拥有的是:

Msg = Msg + "Text goes here for error messages...";

代码:

       private void btnSubmit_Click(object sender, EventArgs e)
    {
//DECLARATIONS
        int count = 0;
        string Msg = "";
        Boolean validatedState = true;
        Boolean validateEntry = false;
        DateTime endDate = new DateTime(2016, 03, 01);
        DateTime startDate = new DateTime(2016, 03, 01);
//BEGIN SERIES OF IF/ELSE FOR CONFIRMING ENTRIES         
        if (Request["txtFirstName"].ToString().Trim() == "")
        {
            //displays yellow bg for missing input
            txtFirstName.BackColor = System.Drawing.Color.Yellow;
            Msg = Msg + "Please Enter a First Name" + "'r'n";
        }//endif
        else
        {
            txtFirstName.BackColor = System.Drawing.Color.White; 
            count += 1;
        }//end else
        if (Request["txtLastName"].ToString().Trim() == "")
        {
            //displays yellow bg for missing input
            txtLastName.BackColor = System.Drawing.Color.Yellow;
            Msg = Msg + "Please Enter a Last Name";
        }//endif
        else
        {
            txtFirstName.BackColor = System.Drawing.Color.White; 
            count += 1;
        }//end else
        if (Request["txtPayRate"].ToString().Trim() == "")
        {
            //displays yellow bg for missing input
            txtPayRate.BackColor = System.Drawing.Color.Yellow;
            Msg = Msg + "Please Enter a Pay Rate";
         }//endif
        else
        {
            txtFirstName.BackColor = System.Drawing.Color.White; 
            count += 1;
        }//end else
        if (Request["txtStartDate"].ToString().Trim() == "")
        {
            //displays yellow bg for missing input
            txtStartDate.BackColor = System.Drawing.Color.Yellow;
            validateEntry = false;
            Msg = Msg + "Please Enter a Start Date";
        }//endif
        else
        {
            startDate = DateTime.Parse(Request["txtStartDate"]);
            validateEntry = true;
        }//end else
        if (Request["txtEndDate"].ToString().Trim() == "")
        {
            //displays yellow bg for missing input
            txtEndDate.BackColor = System.Drawing.Color.Yellow;
            validateEntry = false;
            Msg = Msg + "Please Enter an End Date";
         }//endif
        else
        {
            endDate = DateTime.Parse(Request["txtEndDate"]);
            validateEntry = true;
        }//end else
//END SERIES OF IF/ELSE FOR CONFIRMING ENTRIES 
//START IF VALIDATE ENTRY    
        if (validateEntry == true)
        {
            if (DateTime.Compare(startDate, endDate) >= 0)
            {
                txtStartDate.BackColor = System.Drawing.Color.Yellow;
                txtEndDate.BackColor = System.Drawing.Color.Yellow;
                Msg = Msg + "The end date must be a later date than the start date.";
                //The Msg text will be displayed in lblError.Text after all the error messages are concatenated
                validatedState = false;
                //Boolean value - test each textbox to see if the data entered is valid, if not set validState=false. 
                //If after testing each validation rule, the validatedState value is true, then submit to frmPersonnelVerified.aspx, if not, then display error message
                Response.Write("<span style= 'BackColor:red'>Msg/<span>");
            }
            else //goes to this is dates are correct
            {
                validatedState = true;
                count += 2;
                txtStartDate.BackColor = System.Drawing.Color.White;
                txtEndDate.BackColor = System.Drawing.Color.White;
            }
        }
//END IF VALIDATE ENTRY
//CONFIRMS ALL ARE CORRECT
        if (count == 5 && validatedState == true)
        {
            Session["txtFirstName"] = txtFirstName.Text;
            Session["txtLastName"] = txtLastName.Text;
            Session["txtPayRate"] = txtPayRate.Text;
            Session["txtStartDate"] = txtStartDate.Text;
            Session["txtEndDate"] = txtEndDate.Text;
            Response.Redirect("frmPersonnelVerified.aspx");
            //sends to other page
        }
        else
        {
            Response.Write(Msg);
        }
//ENDS CONFIRMATION OF CORRECT DATA

    }//end Function: private void BtnSubmit_click...
}[![Current Error Response][1]][1]

为表单上的各种错误添加到消息中

如果您认为内置的验证方法不够充分或有效,或者在某种程度上不安全,或者出于任何其他原因,我鼓励您将验证封装在一个委托中,该委托可以在需要时调用,例如:

bool Invalidate(Session session)

确定函数中所需的逻辑,例如会话是否有您需要验证的成员。

如果结果为真,则会话无效,否则为。

派生此工作流程以进一步封装您的需求。

bool InvalidateWorkFlowXYZ(Session session)

此外,模式可以在interface中定义,这将允许您定义任何您想要的附加参数。即

public interface IWorkFlow { System.Action<bool> Invalidate { get; } }

这将允许您将视图逻辑与验证逻辑解耦,而不必处理现有机制或其API范式。

调节