簡體   English   中英

如何要求在vb.net中選中復選框

[英]How to require checkboxes be checked in vb.net

有人可以幫我弄清楚如何在aspx頁面上選中多個復選框嗎? 我發現了一些文章,展示了如何使用JavaScript進行此操作,但我使用VB,但不確定如何應用它。

我想做的是,一旦用戶單擊“提交”按鈕,如果沒有選中足夠多的復選框,它將顯示一個錯誤。 這些不在復選框列表中,而是單個復選框。

您可以為此使用CustomValidator

在ASPX頁面中,將其放入控件和驗證器中。

<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:Label AssociatedControlID="CheckBox1" runat="server">Check this box!</asp:Label>
<asp:CheckBox ID="CheckBox2" runat="server" />
<asp:Label AssociatedControlID="CheckBox2" runat="server">And this box!</asp:Label>

<asp:CustomValidator ID="CustomValidator1" runat="server"
    ErrorMessage="You must check all of the boxes"
    OnServerValidate="CustomValidator1_ServerValidate">
</asp:CustomValidator>

此后,您可以通過檢查ServerValidate事件來檢查他們單擊“ 提交”

Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
    args.IsValid = True ' set default

    If Not CheckBox1.Checked Then
        args.IsValid = False
    End If

    If Not CheckBox2.Checked Then
        args.IsValid = False
    End If
End Sub

ServerValidateEventArgs將允許您指定用戶是否滿足您的條件。

ServerValidate事件結束時,它將返回在屬性IsValid設置的值,以確定它是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM