列表框上的自定义验证器?

时间:2014-03-14 14:55:49

标签: asp.net vb.net validation

在用户能够进入下一页之前,我需要验证我的列表框是否为空。我只是不确定如何实现自定义验证器。以下是我到目前为止:

Protected Sub cmdNext_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) _
    Handles cmdNext.Click

    If StateDropDown.Text = "Select...." Then
        Dim message As String = "Please select a state"
        Dim sb As New System.Text.StringBuilder()
        sb.Append("<script type = 'text/javascript'>")
        sb.Append("window.onload=function(){")
        sb.Append("alert('")
        sb.Append(message)
        sb.Append("')};")
        sb.Append("</script>")
        ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", _
                                               sb.ToString())

        'ElseIf NPIListbox.Text = "" Then
        '    Dim message As String = "Please select a Doctor"
        '    Dim sb As New System.Text.StringBuilder()
        '    sb.Append("<script type = 'text/javascript'>")
        '    sb.Append("window.onload=function(){")
        '    sb.Append("alert('")
        '    sb.Append(message)
        '    sb.Append("')};")
        '    sb.Append("</script>")
        '    ClientScript.RegisterClientScriptBlock(Me.GetType(), "alert", _
        '                                           sb.ToString())
    Else
        Dim data As String = HiddenOptions.Value.TrimEnd(","c)
        Session("NPIList") = data
        Response.Redirect("~/frmDescriptionofNature.aspx")
    End If
End Sub

Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, _
    ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) _
    Handles CustomValidator1.ServerValidate

    args.IsValid = NPIListbox.Items.Count > 0
End Sub

1 个答案:

答案 0 :(得分:-1)

尝试使用此代码

    Protected Sub CustomValidator2_ServerValidate(ByVal source As Object, ByVal args As        System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator2.ServerValidate  
    args.IsValid = False  
    For Each a As ListItem In ListBox1.Items  
        If a.Selected Then  
            args.IsValid = True  
            Exit For  
        End If  
    Next  
End Sub