Checking the checkbox and radio buttons in C #

I use C # for coding!

Below is my html for checkbox and radion button

  <input type="radio" style="float: left;" name="documents" id="Checkbox9" value="yes"
                        runat="server" />
                    <label style="width: 35px!important;" class="checkbox">
                        <%=GetResourceString("c_HSGStudent")%>
                    </label>
                    <input type="radio" style="float: left;" name="documents" id="Checkbox10" value="no"
                        runat="server" />
                    <label style="width: 25px!important;" class="checkbox">
                        <%=GetResourceString("c_HSGParent")%>
                    </label>
                    <input type="radio" style="float: left;" cheked name="documents" id="Radio1" value="yes"
                        runat="server" />
                    <label style="width: 35px!important;" class="checkbox">
                        <%=GetResourceString("c_HSGStudent")%>
                    </label>
                    <input type="radio" style="float: left;" name="documents" id="Radio2" value="no"
                        runat="server" />
                    <label style="width: 25px!important;" class="checkbox">
                        <%=GetResourceString("c_HSGParent")%>
                    </label>

You can see that I have two checkboxes and two radio buttons. My problem is that when I click the submit button, I want to check if the user has checked at least one checkbox or radio button. It would be nice if we could have a .NET solution like (customvalidator).

Please suggest!

thank

+3
source share
2 answers

First add to the CustomValidator page ...

<asp:CustomValidator runat="server" ID="CheckBoxRequired" EnableClientScript="true" 
    OnServerValidate="CheckBoxRequired_ServerValidate" 
    OnClientValidate="CheckBoxRequired_ClientValidate">*</asp:CustomValidator> 

Then you can test them using a client function with a simple jquery call ...

<script type="text/javascript>

function CheckBoxRequired_ClientValidate(sender, e) 
{ 
    e.IsValid = $("input[name='documents']").is(':checked'); 
} 

</script>

server side verification code ...

protected void CheckBoxRequired_ServerValidate(object sender, ServerValidateEventArgs e) 
{ 
    e.IsValid = Checkbox9.Checked || Checkbox10.Checked || Radio1.Checked || Radio2.Checked;
} 
+4
source

, , servervalidate.

0

Source: https://habr.com/ru/post/1744768/


All Articles