添加到表单上的各种错误消息 [英] Adding to a message for various errors on form

查看:128
本文介绍了添加到表单上的各种错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个表单,如果信息没有输入,你会得到一系列错误信息,这取决于不存在的信息。不幸的是,我遇到了一个问题,其中包含消息的变量Msg不起作用(我认为在某一时刻会有意义)。它确实显示错误,但以段落形式显示,不在列表中。



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



我试图将 \ n和\r\\\
无济于事。



我现在拥有的是:

  Msg = Msg + 文字在这里出现错误信息...; 

代码:

  private void btnSubmit_Click(object sender,EventArgs e)
{
// DECLARATIONS
int count = 0;
string Msg =;
布尔validatedState = true;
布尔型validateEntry = false;
DateTime endDate = new DateTime(2016,03,01);
DateTime startDate = new DateTime(2016,03,01);

//开始确认条目IF / ELSE序列
if(Request [txtFirstName]。ToString()。Trim()==)
{
//显示缺少输入的黄色bg
txtFirstName.BackColor = System.Drawing.Color.Yellow;
Msg = Msg +请输入名字+\r\\\
;
} // endif
else
{
txtFirstName.BackColor = System.Drawing.Color.White;
count + = 1;
} // end else
$ b $ if(Request [txtLastName]。ToString()。Trim()==)
{
//显示黄色bg缺少输入
txtLastName.BackColor = System.Drawing.Color.Yellow;
Msg = Msg +请输入姓氏;
} // endif
else
{
txtFirstName.BackColor = System.Drawing.Color.White;
count + = 1;
} // end else
$ b $ if(Request [txtPayRate]。ToString()。Trim()==)
{
//显示黄色bg缺少输入
txtPayRate.BackColor = System.Drawing.Color.Yellow;
Msg = Msg +请输入工资率;
} // endif
else
{
txtFirstName.BackColor = System.Drawing.Color.White;
count + = 1;
} // end else
$ b $ if(Request [txtStartDate]。ToString()。Trim()==)
{
//显示黄色bg缺少输入
txtStartDate.BackColor = System.Drawing.Color.Yellow;
validateEntry = false;
Msg = Msg +请输入开始日期;
} // endif
else
{
startDate = DateTime.Parse(Request [txtStartDate]);
validateEntry = true;
} // end else
$ b $ if(Request [txtEndDate]。ToString()。Trim()==)
{
//显示黄色bg缺少输入
txtEndDate.BackColor = System.Drawing.Color.Yellow;
validateEntry = false;
Msg = Msg +请输入结束日期;
} // endif
else
{
endDate = DateTime.Parse(Request [txtEndDate]);
validateEntry = true;
} // end else
//结束确认条目的IF / ELSE序列

//开始如果验证条目
if(validateEntry == true)
{
if(DateTime.Compare(startDate,endDate)> = 0)
{
txtStartDate.BackColor = System.Drawing.Color.Yellow;
txtEndDate.BackColor = System.Drawing.Color.Yellow;
Msg = Msg +结束日期必须晚于开始日期。;
//所有错误消息连接后,Msg文本将显示在lblError.Text中
validatedState = false;
//布尔值 - 测试每个文本框以查看输入的数据是否有效,如果没有设置validState = false。
//如果在测试每个验证规则之后,validatedState值为true,则提交给frmPersonnelVerified.aspx,如果不是,则显示错误消息
Response.Write(< span style ='BackColor:红'>消息/<跨度>中);
}
else //去这个日期是正确的
{
validatedState = true;
count + = 2;
txtStartDate.BackColor = System.Drawing.Color.White;
txtEndDate.BackColor = System.Drawing.Color.White;
}
}
// END IF VALIDATE ENTRY

//确认所有都是正确的
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);
//发送到其他页面
}
else
{
Response.Write(Msg);
}
//结束正确数据


} // end功能:private void BtnSubmit_click ...

} [ ![Current Error Response] [1]] [1]


解决方案

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

/ p>

bool无效(会话会话)



确定如果会话中有会员需要验证或其他方式,则必须使用逻辑。



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

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

bool InvalidateWorkFlowXYZ )



进一步的模式可能是defin在接口中进行编辑,这将允许您定义所需的任何附加参数。即

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



这将允许您将视图逻辑与验证逻辑分离,而不必处理现有的机制或他们的API范例。

调节


I am attempting to make a form where if information is not input, you get a series of error messages depending on what's not there. Unfortunately, I've run into an issue where the variable Msg that holds the messages is not working (Will make sense in a moment I think). It does display the errors, but in a paragraph form, not in a list.

How can I compile all errors into a message with line breaks?

I've tried to include "\n" and "\r\n" to no avail.

What I have now is this:

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

Code:

       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]

解决方案

If you think the built in validation methods are not sufficient or efficient enough or somehow insecure in some way or for any other reason desire to then I encourage you to encapsulate your validation within a delegate which can be invoked when required for example:

bool Invalidate(Session session)

Determine the logic required in the function such as if the session has the member you require to validate or otherwise.

If the result is true the Session is not valid otherwise it is.

Derive this work flow to encapsulate your requirements further.

bool InvalidateWorkFlowXYZ(Session session)

Further the pattern may be defined within an interface which will allow you to define any additional parameters you desire. i.e.

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

This will allow you to decouple the view logic with the validation logic and not have to deal with the existing mechanisms or their API paradigms.

Reguards

这篇关于添加到表单上的各种错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆