2017-02-04 54 views
0

如何驗證asp.net中的html複選框?
我試過以下。asp.net中html複選框的自定義驗證

<input type="checkbox" id="chk_rule" class="checkbox-custom" name="chk_rule" runat="server"/> 
<label for="bodycontent_chk_rule" class="checkbox-custom-label">I have read and agree to the official rules</label> 
<asp:CustomValidator ID="Agreecheck" runat="server" Display="Dynamic" ForeColor="Red" ErrorMessage="You have to agree the rules." ControlToValidate="chk_rule" OnServerValidate="Agreecheck_ServerValidate"></asp:CustomValidator> 

而服務器端驗證功能如下。

protected void Agreecheck_ServerValidate(object source, ServerValidateEventArgs args) 
{ 
    args.IsValid = chk_rule.Checked; 
} 

但我有以下錯誤。

消息:無法找到由'Agreecheck'的 'ControlToValidate'屬性引用的控件ID'chk_rule'。堆棧跟蹤:在 System.Web.UI.WebControls.BaseValidator.CheckControlValidationProperty(字符串 名,絃樂propertyName的)在 System.Web.UI.WebControls.CustomValidator.ControlPropertiesValid()在 System.Web.UI.WebControls.BaseValidator .OnPreRender(EventArgs e)at System.Web.UI.Control.PreRenderRecursiveInternal()at System.Web.UI.Control.PreRenderRecursiveInternal()at System.Web.UI.Control.PreRenderRecursiveInternal()at System.Web () System.Web.UI.Control.PreRenderRecursiveInternal()at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint)

我該如何解決這個問題?

+0

你使用母版頁嗎? –

+0

請檢查[鏈接](http://www.aspsnippets.com/Articles/ASP.Net-CheckBox-Required-Validation-using-Custom-Validator-and-JavaScript.aspx) –

+0

@FarzInKanz我使用母版頁。 –

回答

0

我測試了你的代碼,如果你用一個文本框替換複選框,它會起作用。我認爲自定義驗證器不適用於複選框。所以,你可以很容易地通過一些jQuery的代碼刪除的問題:

<script> 
    $('input[type=submit]').click(function() 
    { 
     var clientId = '<%= chk_rule.ClientID %>'; 
     if (!$('#' + clientId).is(':checked')) 
     { 
      alert('You must agree the rules'); 
      return false; 
     } 
    }); 
</script> 

下面的代碼片段(僅適用於見),需要上面的代碼爲您的應用程序

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="checkbox" id="chk_rule" class="checkbox-custom" name="chk_rule" runat="server"/> 
 
<label for="bodycontent_chk_rule" class="checkbox-custom-label">I have read and agree to the official rules</label> 
 
<input type="submit" value="submit" id="btnSubmit"> 
 
<script> 
 
     $('input[type=submit]').click(function() 
 
     { 
 
      if (!$('#chk_rule').is(':checked')) 
 
      { 
 
       alert('You must agree the rules'); 
 
       return false; 
 
      } 
 
     }); 
 
</script>

+0

現在我使用母版頁,有必要使用服務器端驗證。
因爲當瀏覽器禁用JavaScript時,客戶端驗證無法工作。 –

+0

沒有差別。我在帶有母版頁的頁面中測試了這一點。 –

+0

禁用javascript時,驗證工作嗎? –

0

您可以使用CustomValidator來查看CheckBox是否被選中。

<asp:CheckBox ID="CheckBox1" runat="server" Text="Check me please" /> 

<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Checkbox not checked" ClientValidationFunction="validateCheckBox"></asp:CustomValidator> 

<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> 

<script type="text/javascript"> 
    function validateCheckBox(sender, args) { 
     args.IsValid = document.getElementById("<%=CheckBox1.ClientID %>").checked; 
    } 
</script> 
+0

如何使用服務器端有效功能? –