验证复选框,单选按钮在C# [英] Validating Checkbox and Radio Button in C#

查看:161
本文介绍了验证复选框,单选按钮在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#进行编码!

下面是我的HTML复选框,并radion按钮

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>

您可以看到我有两个复选框和两个单选按钮,我的问题是,在我提交按钮点击我要检查用户是否已经检查的,至少一个复选框或单选按钮。这将是很好的,如果我们能有像(的CustomValidator).NET解决方案。

You can see I have two checkboxes and two radio buttons, My problem is that on my submit button click I want to check whether user have checked at-least one checkbox or radio button. It will be good if we can have .NET solution like (customvalidator).

请建议!

感谢

推荐答案

首先,的CustomValidator添加到您的网页...

First, add a CustomValidator to your page...

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

然后,您可以再从一个客户端的功能验证他们一个简单的jQuery电话...

You can then then validate them from a client side function with a simple jquery call...

<script type="text/javascript>

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

</script>

code-落后于服务器端验证...

code-behind for server side validation...

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

这篇关于验证复选框,单选按钮在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆