在单个按钮上验证多个服务器端`requiredfieldvalidator`

时间:2014-01-27 12:44:50

标签: vb.net updatepanel requiredfieldvalidator

我已经编写了以下代码......我想要做的就是在单个按钮上指定所有服务器端验证 - ADD 。但它没有使用此代码。我也使用了javascript confirm box,并且不想对此进行验证。

<fieldset style="margin-left: 10%; margin-right: 10%">
            <legend style="text-align: Left; font-size: medium;"><strong>User Details </strong>
            </legend>
            <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" RenderMode="Inline">
                <ContentTemplate>
                    <div align="center">
                        <table style="width: auto; max-width: 100%">
                            <tr>
                                <td colspan="4" style="text-align: right">
                                    <asp:Label runat="server" ID="lbl" Text="* Fields are Mandatory." ForeColor="red"></asp:Label>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: right">
                                    <strong style="font-size: small">Name<label style="color: Red">*</label>
                                        :</strong>
                                </td>
                                <td style="text-align: left">
                                    <asp:TextBox runat="server" ID="txtName" placeholder="Enter Name" TabIndex="1"></asp:TextBox>
                                    <div>
                                        <%--<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional" RenderMode="Inline">
                                            <ContentTemplate>--%>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Please Enter Name!"
                                                    ControlToValidate="txtName"></asp:RequiredFieldValidator>
                                                <asp:RegularExpressionValidator ID="RegularExpressionValidator" runat="server" ErrorMessage="Please Enter Text Only!"
                                                    ControlToValidate="txtName" SetFocusOnError="True" ValidationExpression="^[a-zA-Z]*$"></asp:RegularExpressionValidator>
                                            <%--</ContentTemplate>
                                            <Triggers>
                                                <asp:PostBackTrigger ControlID="btnAdd" />
                                            </Triggers>
                                        </asp:UpdatePanel>--%>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: right">
                                    <strong style="font-size: small">DOB<label style="color: Red">*</label>
                                        :</strong>
                                </td>
                                <td style="text-align: left">
                                    <asp:TextBox runat="server" ID="txtDOB" placeholder="e.g.: 28-Oct-1989" TabIndex="2"></asp:TextBox>
                                    <cc1:CalendarExtender ID="calDOB" runat="server" TargetControlID="txtDOB" Format="dd-MMM-yyyy">
                                    </cc1:CalendarExtender>
                                    <div>
                                        <%--<asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional" RenderMode="Inline">
                                            <ContentTemplate>--%>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidatorDOB" runat="server" ErrorMessage="Please Enter DOB!"
                                                    ControlToValidate="txtDOB"></asp:RequiredFieldValidator>
                                                <asp:RegularExpressionValidator ID="rgeDob" runat="server" ControlToValidate="txtDOB"
                                                    ErrorMessage="Please enter DOB in dd-mmm-yyyy format" SetFocusOnError="True"
                                                    ValidationExpression="^(([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$"
                                                    Display="Dynamic" ForeColor="Red"></asp:RegularExpressionValidator>
                                            <%--</ContentTemplate>
                                            <Triggers>
                                                <asp:PostBackTrigger ControlID="btnAdd" />
                                            </Triggers>
                                        </asp:UpdatePanel>--%>
                                    </div>
                                </td>
                                <td style="text-align: right">
                                    <strong style="font-size: small">Age :</strong>
                                </td>
                                <td style="text-align: left">
                                    <asp:TextBox runat="server" ID="txtAge"></asp:TextBox>
                                    <%--<asp:Label runat="server" ID="lblAge"></asp:Label>--%>
                                </td>
                            </tr>
                            <tr>
                                <td style="text-align: right">
                                    <strong style="font-size: small">Group<label style="color: Red">*</label>
                                        :</strong>
                                </td>
                                <td style="text-align: left">
                                    <asp:DropDownList runat="server" ID="ddlGroup" TabIndex="3">
                                        <asp:ListItem Value="0">Select Value</asp:ListItem>
                                        <asp:ListItem Value="1">Gold</asp:ListItem>
                                        <asp:ListItem Value="2">Silver</asp:ListItem>
                                        <asp:ListItem Value="3">Bronze</asp:ListItem>
                                    </asp:DropDownList>
                                    <div>
                                        <%--<asp:UpdatePanel ID="UpdatePanel4" runat="server" UpdateMode="Conditional" RenderMode="Inline">
                                            <ContentTemplate>--%>
                                                <asp:RequiredFieldValidator ID="RequiredFieldValidator_ddlGroup" runat="server" ErrorMessage="Please Select Group"
                                                    ControlToValidate="ddlGroup"></asp:RequiredFieldValidator>
                                            <%--</ContentTemplate>
                                            <Triggers>
                                                <asp:PostBackTrigger ControlID="btnAdd" />
                                            </Triggers>
                                        </asp:UpdatePanel>--%>
                                    </div>
                                </td>
                                <td style="text-align: Left">
                                    <asp:CheckBox runat="server" ID="chkActive" Text="Active" TabIndex="4" />
                                </td>
                            </tr>
                            <tr>
                                <td colspan="4" align="center">
                                    <asp:Button runat="server" ID="btnAdd" Text="ADD" TabIndex="5" OnClientClick="return fnValidate();" />
                                </td>
                            </tr>
                        </table>
                    </div>
                </ContentTemplate>
                <Triggers>
                    <asp:PostBackTrigger ControlID="btnAdd" />
                </Triggers>
            </asp:UpdatePanel>
        </fieldset>

服务器端代码

Imports Newtonsoft.Json

Partial Class Test_NET_27
    Inherits System.Web.UI.Page

#Region "Variables"
    Private VS_Grid As String = "Null"
#End Region

#Region "PageEvents"
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.txtAge.Attributes.Add("readonly", "readonly")
    End Sub
#End Region

#Region "Button Event"
    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim dt_grid As New DataTable
        Dim dr As DataRow
        Try
            'If Me.txtName.Text = "" Then
            '    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "UpdateSucessScript", "javascript:alert('Please Fill Up All Mandatory(*) Fields!');", True)
            '    Me.txtName.Focus()
            '    Exit Sub
            'ElseIf (Me.txtDOB.Text.ToString.Trim = "") Then
            '    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "UpdateSucessScript", "javascript:alert('Please Fill Up All Mandatory(*) Fields!');", True)
            '    Me.txtDOB.Focus()
            '    Exit Sub
            'ElseIf (Me.ddlGroup.SelectedValue = 0) Then
            '    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "UpdateSucessScript", "javascript:alert('Please Fill Up All Mandatory(*) Fields!');", True)
            '    Me.ddlGroup.Focus()
            '    Exit Sub
            'End If
            Page.Validate()

            If (Me.ViewState(VS_Grid) Is Nothing) Then
                dt_grid.Columns.Add("Name")
                dt_grid.Columns.Add("Group")
                dt_grid.Columns.Add("Active")
                dt_grid.Columns.Add("Age")
                'dt_grid.Columns.Add("Operation")
            Else
                dt_grid = Me.ViewState(VS_Grid)
            End If
            dr = dt_grid.NewRow
            dr("Name") = Me.txtName.Text.ToString.Trim
            dr("Group") = Me.ddlGroup.SelectedItem.Text.ToString.Trim
            dr("Active") = "InActive"
            If Me.chkActive.Checked = True Then
                dr("Active") = "Active"
            End If
            dr("Age") = Me.txtAge.Text.ToString.Trim

            dt_grid.Rows.Add(dr)
            Me.ViewState(VS_Grid) = dt_grid
            If dt_grid.Rows.Count > 0 Then
                Me.gvDetails.DataSource = dt_grid
                Me.gvDetails.DataBind()
            Else
                Me.gvDetails.EmptyDataText = "No Data Available."
                Me.gvDetails.DataSource = Nothing
                Me.gvDetails.DataBind()
            End If
            fnResetValues()
        Catch ex As Exception

        End Try
    End Sub
#End Region

#Region "GridEvents"
    Protected Sub gvDetails_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvDetails.RowCommand
        Dim index As Integer = e.CommandArgument
        Dim dt_grid As New DataTable
        Try
            dt_grid = Me.ViewState(VS_Grid)
            If dt_grid.Rows.Count > 0 Then
                If e.CommandName.ToUpper = "DELETE" Then
                    dt_grid.Rows(index).Delete()

                End If
                dt_grid.AcceptChanges()
                Me.ViewState(VS_Grid) = dt_grid
                Me.gvDetails.DataSource = dt_grid
                Me.gvDetails.DataBind()
            End If
        Catch ex As Exception

        End Try

    End Sub

    Protected Sub gvDetails_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvDetails.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            CType(e.Row.FindControl("lnkDelete"), ImageButton).CommandArgument = e.Row.RowIndex
            CType(e.Row.FindControl("lnkDelete"), ImageButton).CommandName = "DELETE"
        End If
    End Sub

    Protected Sub gvDetails_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvDetails.RowDeleting

    End Sub

#End Region

#Region "PageMethod"
    <Web.Services.WebMethod()> _
       Public Shared Function GetAge(ByVal dob As String) As String

        Dim dt_jsonData As New DataTable
        Dim dr As DataRow = dt_jsonData.NewRow
        Dim age As Integer
        Dim jsonString As String = String.Empty
        Try
            age = Math.Floor(DateDiff(DateInterval.Month, DateValue(dob), Now()) / 12)
            dt_jsonData.Columns.Add("age")
            dr("age") = age.ToString.Trim()
            dt_jsonData.Rows.Add(dr)
            If dt_jsonData.Rows.Count <> 0 Then
                jsonString = JsonConvert.SerializeObject(dt_jsonData)
            End If
            Return jsonString
        Catch ex As Exception
            Return ""
            Exit Function
        End Try
    End Function
#End Region

#Region "Reset Values"
    Public Function fnResetValues() As Boolean
        Me.txtName.Text = ""
        Me.txtDOB.Text = ""
        Me.txtAge.Text = ""
        Me.ddlGroup.SelectedValue = 0
        Me.chkActive.Checked = False
    End Function
#End Region

End Class

当我点击ADD按钮时,它没有执行服务器端验证!

1 个答案:

答案 0 :(得分:0)

您尚未显示服务器端代码...其中应包含Page.Validate()If Page.IsValid