Prop Webcontrol Tablecell Text
## ASP.NET TableCell Text Property
The `Text` property is a fundamental member of the `TableCell` control in ASP.NET Web Forms. It allows developers to programmatically or declaratively set and retrieve the plain text content displayed inside an individual table cell.
---
## Definition and Usage
The `Text` property is used to get or set the text content of a `TableCell` control.
When you set this property, the specified string is rendered directly inside the HTML `` (or ` `) tag. If the cell contains child controls (such as buttons, labels, or textboxes), setting the `Text` property will clear those controls and replace them with the specified text string.
---
## Syntax
### Declarative Syntax
```xml
```
### Property Values
| Property Value | Description |
| :--- | :--- |
| `string` | A string value representing the text content to be displayed inside the table cell. |
---
## Code Examples
### Example 1: Declarative Implementation
The following example demonstrates how to set the `Text` property of a `TableCell` directly within the markup (.aspx file):
```xml
```
### Example 2: Programmatic Implementation (C#)
You can also manipulate the `Text` property dynamically in your code-behind file:
```csharp
// Code-behind (.aspx.cs)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Create a new row
TableRow row = new TableRow();
// Create a cell and set its Text property
TableCell cell = new TableCell();
cell.Text = "Dynamic Cell Content";
// Add the cell to the row, and the row to the table
row.Cells.Add(cell);
tab1.Rows.Add(row);
}
}
```
---
## Important Considerations
1. **HTML Encoding:** The `Text` property does not automatically HTML-encode its value. If you assign HTML tags to the `Text` property (e.g., `cell.Text = "Bold Text"`), the browser will render it as HTML. To prevent Cross-Site Scripting (XSS) vulnerabilities when displaying user input, ensure you use `HttpUtility.HtmlEncode()` before assigning values to this property.
2. **Conflict with Child Controls:** A `TableCell` can contain either text or child controls. If you add controls to the `TableCell.Controls` collection and also set the `Text` property, the `Text` property value will override the child controls during rendering, or vice versa depending on the lifecycle stage. For complex cell layouts, it is recommended to use child controls (like `Literal` or `Label`) instead of the `Text` property.
YouTip