1

I've looked at a dozen previous answers on here nothing has worked for me. I need a form where a checkbox must be checked in order to submit. I am using C# and am pretty new to it, I just found out the RequiredFieldValidator that works with text box's won't work with checkboxes so I need another way to do this.

On submit the data in the fields gets sent to a MS SQL database. I have more fields then the one's shown below but I thought I would condense it for this post and removed everything but one text box and the checkbox I need validated upon submit.

I'd like it to do this on submit:

enter image description here

<form id="form1" runat="server">
    <asp:Label ID="NameLbl" runat="server" Text="Label"></asp:Label><asp:RequiredFieldValidator
        ID="NameRFV" runat="server" ErrorMessage="NameTxtBox"></asp:RequiredFieldValidator>
    <asp:TextBox ID="NameTxtBox" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="AliveLbl" runat="server" Text="Are you alive?"></asp:Label>
    <asp:CheckBox ID="AliveChkBox" runat="server" />
    <asp:Button ID="Submit" class="btn btn-primary" runat="server" Text="Submit"  OnClick="Submit_Click" />
</form>

I've read about using CustomValidator but I can't get anything to function properly with it. People keep saying you need to compile/build your code for it to work and I simply don't no how to do that.

Will consider javascript options as well. I can also use a CheckboxList instead if you know of a better way to validate those.

techora
  • 619
  • 3
  • 18
  • 38

4 Answers4

0

In Submit_Click function, you could add a check for whether AliveChkBox.Checked is true. If it's false, you could show a MessageBox or something like that to inform user that it needs to be checked, then simply return from Submit_Click so that nothing will happen until it's checked.

David S.
  • 5,965
  • 2
  • 40
  • 77
0

You want to use

if (checkbox1.Checked) 
btn1.enabled

else (checkbox1.disabled)
btn1.disabled
this.errorProvider1.SetError(checkbox1, "Must be checked to continue"); 
Philip Gullick
  • 995
  • 7
  • 22
0

Here is the solution:

Code behind:

using System;

public partial class TestCheckBox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "AliveScript", "<script>function CheckAlive(){ return document.getElementById('" + this.AliveChkBox.ClientID + "').checked; } </script>");

        this.Submit.OnClientClick = "return CheckAlive();";
    }
    protected void Submit_Click(object sender, EventArgs e)
    {

    }
}

aspx page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestCheckBox.aspx.cs" Inherits="TestCheckBox" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Label ID="NameLbl" runat="server" Text="Label"></asp:Label>
        <asp:RequiredFieldValidator ID="NameRFV" runat="server" ErrorMessage="NameTxtBox" ControlToValidate="NameTxtBox"></asp:RequiredFieldValidator>
        <asp:TextBox ID="NameTxtBox" runat="server"></asp:TextBox>
        <br />
        <asp:Label ID="AliveLbl" runat="server" Text="Are you alive?"></asp:Label>
        <asp:CheckBox ID="AliveChkBox" runat="server" />
        <asp:Button ID="Submit" class="btn btn-primary" runat="server" Text="Submit" OnClick="Submit_Click" />
    </form>
</body>
</html>
mtsiakiris
  • 190
  • 9
0

if (checkBox1.Checked == true) button1.Enabled = true;

Use a code similar to this, just make it so button1 is not enabled on the properties

JayH
  • 194
  • 1
  • 3
  • 17