Prop Webcontrol Checkbox Checked
## ASP.NET Web Control: CheckBox Checked Property
The `Checked` property is a fundamental property of the ASP.NET Web Server `CheckBox` control. It is used to get or set a value indicating whether the `CheckBox` control is selected (checked).
---
## Definition and Usage
The `Checked` property determines the state of the `CheckBox` control:
* When set to `true`, the checkbox is checked (selected).
* When set to `false`, the checkbox is unchecked (cleared).
By default, the `Checked` property is set to `false`.
---
## Syntax
You can declare the `Checked` property declaratively in your `.aspx` markup or programmatically in your code-behind (C# or VB.NET).
### Declarative Syntax
```xml
```
### Programmatic Syntax (C#)
```csharp
// Get the state of the checkbox
bool isChecked = myCheckBox.Checked;
// Set the state of the checkbox
myCheckBox.Checked = true;
```
---
## Code Examples
### Example 1: Basic Declarative Usage
The following example demonstrates how to pre-check a `CheckBox` control when the page loads for the first time.
```xml
```
### Example 2: Interactive Code-Behind Example
This example shows how to read the state of the `Checked` property during a postback event (e.g., when a button is clicked) and display a message to the user.
#### ASPX Markup (`Default.aspx`):
```xml
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourApp.Default" %>
CheckBox Checked Property Example
```
#### Code-Behind (`Default.aspx.cs`):
```csharp
using System;
namespace YourApp
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Optional: Initialize checkbox state on initial page load
if (!IsPostBack)
{
lblMessage.Text = "Please make a selection.";
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Check the state of the Checked property
if (chkSubscribe.Checked)
{
lblMessage.Text = "Thank you! You have successfully subscribed to our newsletter.";
lblMessage.ForeColor = System.Drawing.Color.Green;
}
else
{
lblMessage.Text = "You opted out of the newsletter subscription.";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
}
}
```
---
## Key Considerations
1. **AutoPostBack Interaction**: By default, changing the checked state of a checkbox on the client side does not immediately trigger a postback to the server. If you want the server-side code to execute immediately when a user clicks the checkbox, you must set the `AutoPostBack` property to `true` and handle the `OnCheckedChanged` event.
2. **State Persistence**: The state of the `Checked` property is automatically maintained across postbacks using ASP.NET ViewState, provided ViewState is enabled for the control or page.
3. **Boolean Values in Markup**: In the `.aspx` markup, the `Checked` attribute accepts string representations of booleans (e.g., `"true"`, `"false"`, `"TRUE"`, or `"FALSE"`). In the C# code-behind, it strictly requires a boolean type (`true` or `false`).
YouTip