CheckBox CausesValidation Property C# Example

The CausesValidation Property of CheckBox control accepts a Boolean value as true or false to indicate whether to perform validation tests over the server controls which accept any mandatory or specific type of input. If the value of CausesValidation property is set to true then it performs the validation test for the validated server controls placed on the web form before sending the input data to the server. For CheckBox control the CausesValidation property works only if the AutoPostBack property is set to true. In this example you will learn how to specify the value for CausesValidation property in the HTML code of CheckBox control as an inline attribute. You will also learn the dynamic way of assigning the value to the CausesValidation property using C# code programmatically.

HTML Code Example

<table border="0" cellpadding="5" cellspacing="0" width="500" style="text-align: left"
    align="center">
    <tr>
        <td>
            <asp:Label ID="Label3" runat="server" Font-Bold="true" Text="Enter some text or leave it blank:"></asp:Label>
        </td>
        <td>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" 
                runat="server" 
                ErrorMessage="RequiredFieldValidator" 
                ControlToValidate="TextBox1">
            </asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td>
            <asp:Label ID="Label1" runat="server" Font-Bold="true" Text="Click the CheckBox:"></asp:Label>
        </td>
        <td>
            <asp:CheckBox ID="CheckBox1" 
                runat="server" 
                Text="CheckBox 1" 
                CausesValidation="true" 
                AutoPostBack="true" />
        </td>
    </tr>
</table>

Alternatively, you can also perform the same task by assigning the property value programmatically as shown in the code below.

C# Code Example

CheckBox1.CausesValidation = true;