当用户尝试发送表单而不勾选复选框时,我需要向对应的复选框显示一个星号...
我找到了类似这样的东西但是没有用,就像普通的其他<asp:RequiredFieldValidator
字段一样:
<asp:CustomValidator ID="CheckBoxValidator" runat="server" onservervalidate="CheckBoxRequired_ServerValidate" Display="Dynamic" CssClass="error-text" ErrorMessage='*' ValidationGroup="save"> * </asp:CustomValidator>
asp:CheckBox ID =&#34; YourCheckBox&#34; RUNAT =&#34;服务器&#34;文本=&#34;我接受你的条款和条件bla bla bla&#34; /&GT;
在我背后的代码中:
protected void CheckBoxRequired_ServerValidate(object sender, ServerValidateEventArgs e)
{
e.IsValid = YourCheckBox.Checked;
labelMessage.Text = "Your reservation has been processed.";
}
答案 0 :(得分:0)
看看这篇博文。这就是你需要的东西
[编辑]
我在这里对链接
中的代码进行了一些修改<script type = "text/javascript">
function ValidateCheckBox(sender, args) {
if (document.getElementById("CheckBox1").checked == true) {
args.IsValid = true;
} else {
args.IsValid = false;
}
}
</script>
<asp:CheckBox ID="CheckBox1" ClientIDMode="Static" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="CustomValidator" EnableClientScript="true" ValidationGroup="ggg" ClientValidationFunction="ValidateCheckBox"></asp:CustomValidator>
<asp:Button ID="Button1" runat="server" Text="Submit" ValidationGroup="ggg" />
[编辑2] 要仅在服务器上进行验证,请删除 ClientValidationFunction 和 ValidationGroup 并执行此操作
protected void Button1_Click(object sender, EventArgs e)
{
if (!CheckBox1.Checked)
{
CustomValidator1.IsValid = false;
}
}