我在这个问题上做了一些搜索,我找到了几个部分答案,但是没有一个让我感到温暖模糊的“这是做这件事的正确方式”。为了回答对这个问题最常见的抱怨:“复选框可以有两种合法的状态-选中和未选中”,这是一个“我接受条款和条件...”为了完成注册而必须选中的复选框,因此从业务逻辑的角度来看,需要选中该复选框。
请在您的回复中提供完整的可剪切粘贴的代码片段!我知道这其中有几个部分-- CustomValidator (可能)、代码隐藏、一些javascript和可能的IsValid检查,令我沮丧的是,在我看到的每个示例中,都缺少这些关键部分中的一个!
发布于 2009-08-04 15:37:59
客户端验证的javascript函数(使用jQuery)...
function CheckBoxRequired_ClientValidate(sender, e)
{
e.IsValid = jQuery(".AcceptedAgreement input:checkbox").is(':checked');
}
服务器端验证的代码隐藏...
protected void CheckBoxRequired_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = MyCheckBox.Checked;
}
复选框验证器的ASP.Net代码...
<asp:CheckBox runat="server" ID="MyCheckBox" CssClass="AcceptedAgreement" />
<asp:CustomValidator runat="server" ID="CheckBoxRequired" EnableClientScript="true"
OnServerValidate="CheckBoxRequired_ServerValidate"
ClientValidationFunction="CheckBoxRequired_ClientValidate">You must select this box to proceed.</asp:CustomValidator>
最后,在你的回发中--无论是通过按钮还是其他什么……
if (Page.IsValid)
{
// your code here...
}
发布于 2009-08-04 15:39:06
安德鲁答案的C#版本:
<asp:CustomValidator ID="CustomValidator1" runat="server"
ErrorMessage="Please accept the terms..."
onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>
<asp:CheckBox ID="CheckBox1" runat="server" />
代码隐藏:
protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = CheckBox1.Checked;
}
发布于 2011-12-07 10:21:55
如果您想要一个真正的验证器,它不依赖于jquery,并且还可以处理服务器端验证(您应该这样做。服务器端验证是最重要的部分),下面是一个控件
public class RequiredCheckBoxValidator : System.Web.UI.WebControls.BaseValidator
{
private System.Web.UI.WebControls.CheckBox _ctrlToValidate = null;
protected System.Web.UI.WebControls.CheckBox CheckBoxToValidate
{
get
{
if (_ctrlToValidate == null)
_ctrlToValidate = FindControl(this.ControlToValidate) as System.Web.UI.WebControls.CheckBox;
return _ctrlToValidate;
}
}
protected override bool ControlPropertiesValid()
{
if (this.ControlToValidate.Length == 0)
throw new System.Web.HttpException(string.Format("The ControlToValidate property of '{0}' is required.", this.ID));
if (this.CheckBoxToValidate == null)
throw new System.Web.HttpException(string.Format("This control can only validate CheckBox."));
return true;
}
protected override bool EvaluateIsValid()
{
return CheckBoxToValidate.Checked;
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
if (this.Visible && this.Enabled)
{
System.Web.UI.ClientScriptManager cs = this.Page.ClientScript;
if (this.DetermineRenderUplevel() && this.EnableClientScript)
{
cs.RegisterExpandoAttribute(this.ClientID, "evaluationfunction", "cb_verify", false);
}
if (!this.Page.ClientScript.IsClientScriptBlockRegistered(this.GetType().FullName))
{
cs.RegisterClientScriptBlock(this.GetType(), this.GetType().FullName, GetClientSideScript());
}
}
}
private string GetClientSideScript()
{
return @"<script language=""javascript"">function cb_verify(sender) {var cntrl = document.getElementById(sender.controltovalidate);return cntrl.checked;}</script>";
}
}
https://stackoverflow.com/questions/1228112
复制相似问题