如何驗證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)
我該如何解決這個問題?
你使用母版頁嗎? –
請檢查[鏈接](http://www.aspsnippets.com/Articles/ASP.Net-CheckBox-Required-Validation-using-Custom-Validator-and-JavaScript.aspx) –
@FarzInKanz我使用母版頁。 –