Prop Webcontrol Style Cssclass
## ASP.NET WebControl CssClass Property
The `CssClass` property is a fundamental styling property used in ASP.NET Web Forms. It allows developers to apply CSS (Cascading Style Sheets) classes to server controls, enabling a clean separation of presentation (CSS) from application logic and markup.
---
## Definition and Usage
The `CssClass` property gets or sets the CSS class rendered by the Web server control on the client browser.
When the ASP.NET engine renders the page into HTML, the value of the `CssClass` property is compiled into the standard HTML `class` attribute of the corresponding HTML element.
---
## Syntax
```xml
```
### Property Values
| Property Value | Description |
| :--- | :--- |
| `style-class-name` | A string value specifying one or more CSS class names to apply to the control. Multiple classes can be separated by spaces (e.g., `CssClass="btn btn-primary"`). |
---
## Code Example
The following example demonstrates how to define a CSS class in a `
```
### Rendered HTML Output
When the page is requested by a browser, ASP.NET renders the server control as standard HTML:
```html
```
---
## Key Considerations
### 1. Applying Multiple Classes
You can apply multiple CSS classes to a single control by separating the class names with a space, just as you would in standard HTML:
```xml
```
### 2. Programmatic Manipulation
You can dynamically get or set the `CssClass` property in your code-behind file (C# or VB.NET) during page lifecycle events (such as `Page_Load`):
**C#:**
```csharp
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Button1.CssClass = "TestStyle";
}
}
```
**VB.NET:**
```vb
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Button1.CssClass = "TestStyle"
End If
End Sub
```
### 3. CssClass vs. Inline Styles
While you can use individual style properties (like `ForeColor`, `BackColor`, `Font-Bold`) directly on an ASP.NET control, using `CssClass` is highly recommended for production environments. It keeps your markup clean, reduces page weight, and ensures consistency across your web application.
YouTip