为什么服务器验证不能在我的代码中工作?

时间:2022-03-16 21:17:00

Below is the code of my simple page's submit button click. I am submitting an empty form but no server validation error messages are showing. Whats wrong with my code? Please help. WHen I click on submit, the page just turns blank and nothing happens. I am also unable to attach the debugger. When I am building my website project, it is not showing any compilation error either. I dont know what I am doing wrong.

下面是我的简单页面提交按钮单击的代码。我正在提交一个空表单,但是没有显示服务器验证错误消息。我的代码有什么问题吗?请帮助。当我点击提交时,页面会变成空白,什么都不会发生。我也无法附加调试器。当我在构建我的网站项目时,它也没有显示任何编译错误。我不知道我做错了什么。

 protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // Need to Validate All Required Fields before redirecting to frmPersonalVerified.aspx
            bool blnFormIsValid = true;
            DateTime dtEndDate;
            DateTime dtStartDate;

            // Get Date because we have a value.
            dtEndDate = DateTime.Parse(txtEndDate.Text);

            // Get Date because we have a value.
            dtStartDate = DateTime.Parse(txtStartDate.Text);

            if (txtFirstName.Text.Trim() == "")
            {
                txtFirstName.BackColor = System.Drawing.Color.Yellow;
                lblError.Text = "Please enter first name.";
                blnFormIsValid = false;
            }
            else
            {
                lblError.Text = "";
                txtFirstName.BackColor = System.Drawing.Color.White;
                blnFormIsValid = true;
            }

            if (txtLastName.Text.Trim() == "")
            {
                txtLastName.BackColor = System.Drawing.Color.Yellow;
                lblError.Text = "Please enter last name.";
                blnFormIsValid = false;
            }
            else
            {
                lblError.Text = "";
                txtLastName.BackColor = System.Drawing.Color.White;
                blnFormIsValid = true;
            }
            if (txtPayRate.Text.Trim() == "")
            {
                txtPayRate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text = "Please enter pay rate.";
                blnFormIsValid = false;
            }
            else
            {
                lblError.Text = "";
                txtPayRate.BackColor = System.Drawing.Color.White;
                blnFormIsValid = true;
            }
            if (txtStartDate.Text.Trim() == "")
            {
                txtStartDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text = "Please enter start date.";
                blnFormIsValid = false;
            }
            else
            {
                lblError.Text = "";
                txtStartDate.BackColor = System.Drawing.Color.White;
                blnFormIsValid = true;
            }
            if (txtEndDate.Text.Trim() == "")
            {
                txtEndDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text = "Please enter end date.";
                blnFormIsValid = false;
            }
            else
            {
                lblError.Text = "";
                txtEndDate.BackColor = System.Drawing.Color.White;
                blnFormIsValid = true;
            }

            // Compare Dates
            if (DateTime.Compare(dtStartDate, dtEndDate) >= 0)
            {
                txtStartDate.BackColor = System.Drawing.Color.Yellow;
                txtEndDate.BackColor = System.Drawing.Color.Yellow;
                lblError.Text = "Please make sure that start date is less than end date.";
                blnFormIsValid = false;
            }

            else
            {
                lblError.Text = "";
                txtStartDate.BackColor = System.Drawing.Color.White;
                txtEndDate.BackColor = System.Drawing.Color.White;
                blnFormIsValid = true;
            }

            if (blnFormIsValid == true)
            {
                //Assign a value to the session variable. 
                Session["FirstName"] = txtFirstName.Text;
                Session["LastName"] = txtLastName.Text;
                Session["PayRate"] = txtPayRate.Text;
                Session["StartDate"] = txtStartDate.Text;
                Session["EndDate"] = txtEndDate.Text;

                // Sends A Request from the Browser to the server.
                Response.Redirect("frmPersonalVerified.aspx");
            }
        }

@Lion

@Lion

I just used .Equals("")...its not working...still blank page is showing up

我只是用.Equals(" ")…它不工作…仍然出现空白页

1 个解决方案

#1


1  

Ignoring the fact that the correct way is to use ASP.NET's built-in validation tools, the problem is that your program's logic is broken.

忽略正确的方法是使用ASP这一事实。NET内置的验证工具,问题是你的程序逻辑被破坏了。

You're using blnFormIsValid to store the validity of the form, however its value is meaningless because you're assigning it without paying attention to previous state.

您正在使用blnFormIsValid来存储表单的有效性,但是它的值没有意义,因为您在分配它时没有注意到以前的状态。

If I submit your page's form with these values...

如果我提交你的页面的表单与这些值……

txtFirstName = "" // this is invalid
txtLastName = "foo" // this is valid

...then it will correctly fail the first validation and blnFormIsValid will be false, however your next check ignores the state of blnFormIsValid and sets it to true simply because txtLastName's value is valid.

…然后,它将正确地失败第一次验证,而blnFormIsValid将是false,但是您的下一个检查将忽略blnFormIsValid的状态,并将其设置为true,因为txtLastName的值是有效的。

This issue stems not from our lack of understanding or knowledge of ASP.NET, but basic programming and logic. A simple step-through or dry-run of your code would have revealed this.

这个问题不是因为我们缺乏对ASP的理解和认识。NET,但是基本的编程和逻辑。一个简单的代码的逐步运行或运行将揭示这一点。

Below are my list of recommendations:

以下是我的建议清单:

Use ASP.NET Validation controls, like so:

<input type="text" id="firstName" runat="server" />
<asp:RequiredValidator runat="server" controlToValidate="firstName" />

void Page_Load() {
    if( Page.IsPostBack) {
        Page.Validate();
        if( Page.IsValid ) {
            // that's all you have to do
        }
    }

Don't use Hungarian notation

This is when you prefix an identifier with a tag that identifies its type, e.g. "blnFormIsValid" or "txtFirstName". Just use "formIsValid" or "firstName". Hungarian notation is only of use in environments where typing information is not provided by the editor.

这是当您在标识符前面加上标识其类型的标记时。“blnFormIsValid”或“txtFirstName”。只需使用“formIsValid”或“firstName”。匈牙利表示法只是在没有编辑器提供输入信息的环境中使用。

Don't use foo == true

... because the operation will evaluate to the same value as foo. In your case you should have if( formIsValid ) instead of if( formIsValid == true ). Avoiding unnecessary use of the == operator can help avoid cases where you accidentally use the = assignment operator instead of the == equality operator (and make your code more readable).

…因为操作将计算与foo相同的值。在您的例子中,应该有if(formIsValid)而不是if(formIsValid == true)。避免不必要地使用==操作符可以帮助避免不小心使用=赋值操作符而不是== =等号操作符(并使代码更具可读性)的情况。

#1


1  

Ignoring the fact that the correct way is to use ASP.NET's built-in validation tools, the problem is that your program's logic is broken.

忽略正确的方法是使用ASP这一事实。NET内置的验证工具,问题是你的程序逻辑被破坏了。

You're using blnFormIsValid to store the validity of the form, however its value is meaningless because you're assigning it without paying attention to previous state.

您正在使用blnFormIsValid来存储表单的有效性,但是它的值没有意义,因为您在分配它时没有注意到以前的状态。

If I submit your page's form with these values...

如果我提交你的页面的表单与这些值……

txtFirstName = "" // this is invalid
txtLastName = "foo" // this is valid

...then it will correctly fail the first validation and blnFormIsValid will be false, however your next check ignores the state of blnFormIsValid and sets it to true simply because txtLastName's value is valid.

…然后,它将正确地失败第一次验证,而blnFormIsValid将是false,但是您的下一个检查将忽略blnFormIsValid的状态,并将其设置为true,因为txtLastName的值是有效的。

This issue stems not from our lack of understanding or knowledge of ASP.NET, but basic programming and logic. A simple step-through or dry-run of your code would have revealed this.

这个问题不是因为我们缺乏对ASP的理解和认识。NET,但是基本的编程和逻辑。一个简单的代码的逐步运行或运行将揭示这一点。

Below are my list of recommendations:

以下是我的建议清单:

Use ASP.NET Validation controls, like so:

<input type="text" id="firstName" runat="server" />
<asp:RequiredValidator runat="server" controlToValidate="firstName" />

void Page_Load() {
    if( Page.IsPostBack) {
        Page.Validate();
        if( Page.IsValid ) {
            // that's all you have to do
        }
    }

Don't use Hungarian notation

This is when you prefix an identifier with a tag that identifies its type, e.g. "blnFormIsValid" or "txtFirstName". Just use "formIsValid" or "firstName". Hungarian notation is only of use in environments where typing information is not provided by the editor.

这是当您在标识符前面加上标识其类型的标记时。“blnFormIsValid”或“txtFirstName”。只需使用“formIsValid”或“firstName”。匈牙利表示法只是在没有编辑器提供输入信息的环境中使用。

Don't use foo == true

... because the operation will evaluate to the same value as foo. In your case you should have if( formIsValid ) instead of if( formIsValid == true ). Avoiding unnecessary use of the == operator can help avoid cases where you accidentally use the = assignment operator instead of the == equality operator (and make your code more readable).

…因为操作将计算与foo相同的值。在您的例子中,应该有if(formIsValid)而不是if(formIsValid == true)。避免不必要地使用==操作符可以帮助避免不小心使用=赋值操作符而不是== =等号操作符(并使代码更具可读性)的情况。