验证失败但转到下一页

时间:2009-11-17 16:23:53

标签: .net asp.net visual-studio-2008 validation

我在一个页面中有正则表达式验证,它检查文本框中输入的值是否为数字,它不是数字,它给出错误消息“它应该是数字”

但是当我点击表单的下一个按钮时,它会导航到下一页。但它不应该。怎么解决这个问题?

 Protected Sub btn_View1_Next_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_View1_Next.Click
        Try
            Dim currentView As Int16
            currentView = mvRequestorForm.ActiveViewIndex
            Dim rowsCount As Integer = gvRoleDepartment.Rows.Count
            Dim checkCount As Integer = 0
            ''USERID Validation
            lblUserExists.Text = String.Empty

            rfvStoreNumber.Validate()
            rfvStoreNumber.SetFocusOnError = True

            rfvSurName.Validate()
            rfvSurName.SetFocusOnError = True

            ''New USesr
            If (ddl_view0_typeOfRequest.SelectedItem.ToString().ToLower().Equals("new")) Then
                rfvEmplyeeNumber.Visible = True
                If Page.IsValid Then
                    If currentView = 1 Then
                        mvRequestorForm.ActiveViewIndex = (currentView + 1)
                        rfv_view2_managersEmail.Enabled = True
                        rev_view2_managersEmail.Enabled = True
                    Else
                        mvRequestorForm.ActiveViewIndex = (currentView + 1)
                    End If

                    If currentView = 0 Then accountType = ddl_view0_typeOfRequest.SelectedItem.ToString()
                    If mvRequestorForm.ActiveViewIndex = 1 Then
                        Sub_ActivateView1()
                    End If
                End If
            ElseIf ddl_view0_typeOfRequest.SelectedValue.ToString().ToLower().Equals("delete") Then
                ''Check the requirement

                'rfvFirstName.Validate()
                'rfvFirstName.SetFocusOnError = True

                rfvUserID.Validate()
                rfvUserID.SetFocusOnError = True

                rfvEmplyeeNumber.Visible = True
                If Page.IsValid Then
                    Dim dsRoles As New DataSet
                    dsRoles = SearchGroups(txtUserID.Text.Trim())
                    If Not sResult Is Nothing Then
                        lblUserExists.Text = String.Empty
                        If currentView = 1 Then
                            mvRequestorForm.ActiveViewIndex = (currentView + 1)
                            rfv_view2_managersEmail.Enabled = True
                            rev_view2_managersEmail.Enabled = True
                        Else
                            mvRequestorForm.ActiveViewIndex = (currentView + 1)
                        End If

                        If currentView = 0 Then accountType = ddl_view0_typeOfRequest.SelectedItem.ToString()
                        If mvRequestorForm.ActiveViewIndex = 1 Then
                            Sub_ActivateView1()
                        End If
                    Else
                        lblUserExists.Text = String.Format("This userid doesn't exist. Please enter a valid user id.")
                        Return
                    End If
                End If
            Else
                ''Update User
                rfvUserID.Validate()
                rfvUserID.SetFocusOnError = True
                rfvEmplyeeNumber.Visible = True
                If Page.IsValid Then
                    Dim dsRoles As New DataSet
                    dsRoles = SearchGroups(txtUserID.Text.Trim())
                    If Not sResult Is Nothing Then
                        lblUserExists.Text = String.Empty
                        If currentView = 1 Then
                            mvRequestorForm.ActiveViewIndex = (currentView + 1)
                            rfv_view2_managersEmail.Enabled = True
                            rev_view2_managersEmail.Enabled = True
                        Else
                            mvRequestorForm.ActiveViewIndex = (currentView + 1)
                        End If

                        If currentView = 0 Then accountType = ddl_view0_typeOfRequest.SelectedItem.ToString()
                        If mvRequestorForm.ActiveViewIndex = 1 Then
                            Sub_ActivateView1()
                        End If
                    Else
                        lblUserExists.Text = String.Format("This userid doesn't exist. Please enter a valid user id.")
                        Return
                    End If
                End If
            End If

        Catch ex As Exception

        End Try
    End Sub

1 个答案:

答案 0 :(得分:1)

首先......你在web.config中有这个吗?

<xhtmlConformance mode="Legacy">

如果你这样做......这基本上打破了所有客户端验证。请务必注意,服务器端验证不会自动阻止您的页面处理。您必须检查Page.IsValid属性......看起来您正在这样做。但...

第二...为什么你在rfvUserID控件上显式调用Validate()?这不应该是必要的。这可能是你通过这样做使IsValid属性无效。

但是,听起来自动验证器没有触发。你可以发布aspx吗?只有“Form2”验证组中的验证器才会触发btn_View1_Next。尝试删除所有ValidationGroup设置。它运作正常吗?

最后...... id“Form2”意味着可疑的东西。您在此页面上只有一个实际的表单标签,对吧?否则你会遇到很多问题。