2010-05-10 83 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事件處理程序的標準。