YouTip LogoYouTip

Prop Webcontrol Tablecell Columnspan

## ASP.NET TableCell ColumnSpan Property The `ColumnSpan` property is a member of the `TableCell` Web Control in ASP.NET. It is used to define or retrieve the number of columns in a `Table` control that a single table cell should span horizontally. This property is the server-side equivalent of the standard HTML `colspan` attribute. --- ## Definition and Usage When designing dynamic tables in ASP.NET, you may need a cell to stretch across multiple columns (for example, a header cell or a full-width banner row). The `ColumnSpan` property allows you to merge adjacent cells in the same row into a single cell. * **Default Value:** `0` (indicates that the property is not set, which defaults to a span of 1 column). * **Value Type:** `Integer` (must be a positive integer). --- ## Syntax ### Declarative Syntax ```xml ``` ### Property Values | Attribute / Property | Type | Description | | :--- | :--- | :--- | | **num** | `Integer` | Specifies the number of columns the `TableCell` should span. Must be a positive integer greater than or equal to 1. | --- ## Code Example The following example demonstrates how to set the `ColumnSpan` property to `"2"`. In this table, the cell in the first row spans across two columns, while the second row contains two individual cells. ```xml <%@ Page Language="C#" %> TableCell ColumnSpan Example

TableCell ColumnSpan Example

<%-- Row 1: Contains a cell that spans 2 columns --%> Spanning 2 Columns <%-- Row 2: Contains two standard cells --%> Column 1 Content Column 2 Content
``` --- ## Programmatic Usage (C#) You can also set or modify the `ColumnSpan` property dynamically in your code-behind file: ```csharp // Create a new TableCell TableCell cell = new TableCell(); cell.Text = "This cell spans 3 columns"; // Set the ColumnSpan property programmatically cell.ColumnSpan = 3; // Add the cell to a TableRow TableRow row = new TableRow(); row.Cells.Add(cell); // Add the row to your Table control (assuming 'myTable' exists on the .aspx page) myTable.Rows.Add(row); ``` --- ## Key Considerations 1. **Layout Consistency:** When using `ColumnSpan`, ensure that the sum of individual cells and spanned cells matches across all rows to maintain a consistent table grid layout. For example, if Row 1 has a cell with `ColumnSpan="3"`, Row 2 should ideally have 3 individual cells (or combinations that add up to 3) to prevent rendering issues. 2. **Browser Compatibility:** The `ColumnSpan` property renders directly as the `colspan` attribute in the output HTML `` tag, which is fully supported by all modern web browsers. 3. **Value Range:** Setting `ColumnSpan` to a value less than `1` will result in an `ArgumentOutOfRangeException` at runtime.
← Prop Webcontrol Tablecell HoriProp Webcontrol Table Horizont β†’