2010-05-10 91 views
3

我正在使用c#进行编码!在C中验证复选框和单选按钮#

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

<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> 

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

请建议!

感谢

回答

4

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

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

然后,您可以再从一个简单的jQuery调用客户端的功能验证它们...

<script type="text/javascript> 

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

</script> 

代码 - 后面为服务器端验证...

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

创建一个自定义的验证,然后请检查是否控制满足的ServerValidate事件处理程序的标准。