Prop Webcontrol Checkbox Text
## ASP.NET CheckBox Text Property
The `Text` property is a fundamental property of the ASP.NET Web Server `CheckBox` control. It is used to set or retrieve the text label associated with the checkbox.
---
## Definition and Usage
The `Text` property defines the label that appears next to the `CheckBox` control on the web page. This text provides context to the user about what option they are selecting.
By default, the text is displayed to the right of the checkbox. However, this alignment can be customized using the `TextAlign` property of the `CheckBox` control.
---
## Syntax
### Declarative Syntax
```asp
```
### Programmatic Syntax (C#)
```csharp
// Get the text
string currentText = myCheckBox.Text;
// Set the text
myCheckBox.Text = "Accept Terms and Conditions";
```
### Property Values
| Value | Description |
| :--- | :--- |
| `string` | A string value representing the text label displayed next to the checkbox. |
---
## Code Examples
### Example 1: Basic Declarative Implementation
The following example demonstrates how to set the `Text` property directly within the ASP.NET markup.
```html
```
### Example 2: Dynamic Text Manipulation (Code-Behind)
This example shows how to dynamically change the `Text` property of a `CheckBox` control using C# code-behind based on user interaction.
**ASPX Markup:**
```html
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
CheckBox Text Example
```
**C# Code-Behind (`Default.aspx.cs`):**
```csharp
using System;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Optionally initialize the text on first load
chkSubscribe.Text = "Yes, sign me up for the newsletter!";
}
}
protected void chkSubscribe_CheckedChanged(object sender, EventArgs e)
{
if (chkSubscribe.Checked)
{
lblStatus.Text = "Status: Subscribed!";
// Dynamically update the checkbox text based on state
chkSubscribe.Text = "Unsubscribe from newsletter";
}
else
{
lblStatus.Text = "Status: Unsubscribed";
chkSubscribe.Text = "Yes, sign me up for the newsletter!";
}
}
}
```
---
## Technical Considerations
1. **HTML Rendering**: When rendered to the browser, the ASP.NET `CheckBox` control generates an HTML `` element wrapped inside a `` tag, with a `
YouTip