YouTip LogoYouTip

Control Listcontrol

## ASP.NET ListControl Class The `ListControl` class serves as the abstract base class for all ASP.NET Web server controls that present a list of items to the user. It defines the core properties, methods, and events shared by all list-based controls. Controls that inherit from `ListControl` include: * **`CheckBoxList`**: Displays a list of checkboxes. * **`DropDownList`**: Displays a single-selection drop-down list. * **`ListBox`**: Displays a single- or multi-selection list box. * **`RadioButtonList`**: Displays a list of mutually exclusive radio buttons. The properties of the `ListControl` class allow you to easily bind to data sources, format display text, manage selections, and handle user interactions. --- ## Properties The following table details the key properties of the `ListControl` class: | Property | Description | Introduced (.NET) | | :--- | :--- | :--- | | **`AppendDataBoundItems`** | Gets or sets a boolean value indicating whether to clear existing list items before binding data. | 2.0 | | **`AutoPostBack`** | Gets or sets a value indicating whether an automatic postback to the server occurs when the user changes the selection in the list. | 1.0 | | **`CausesValidation`** | Specifies whether validation is performed on the page when an item in the list control is clicked. | 2.0 | | **`DataTextField`** | Gets or sets the specific field of the data source that provides the text content for the list items. | 1.0 | | **`DataTextFormatString`** | Gets or sets the formatting string used to control how the data bound to the list control is displayed. | 1.0 | | **`DataValueField`** | Gets or sets the specific field of the data source that provides the value for each list item. | 1.0 | | **`Items`** | Gets the collection of items (`ListItemCollection`) within the list control. | 1.0 | | **`runat`** | Specifies that the control is a server-side control. Must be set to `"server"`. | 1.0 | | **`SelectedIndex`** | Gets or sets the lowest ordinal index of the selected item in the list. Returns `-1` if nothing is selected. | 1.0 | | **`SelectedItem`** | Gets the selected item with the lowest index in the list control. | 1.0 | | **`SelectedValue`** | Gets or sets the value of the selected item in the list control, or selects the item in the list control that contains the specified value. | 1.0 | | **`TagKey`** | Gets the HTML tag that corresponds to the list control. | 1.0 | | **`Text`** | Gets or sets the `SelectedValue` of the list control. | 2.0 | | **`ValidationGroup`** | Gets or sets the group of controls for which the list control causes validation when it posts back to the server. | 2.0 | | **`OnSelectedIndexChanged`** | Specifies the name of the event handler method executed when the selected index of the list control changes. | 1.0 | --- ## Code Examples ### 1. Declarative Data Binding with DropDownList The following example demonstrates how to bind a `DropDownList` (which inherits from `ListControl`) to a data source using declarative properties like `DataTextField` and `DataValueField`. ```html <%@ Page Language="C#" %> ListControl Data Binding Example

Select a Department:



``` ### 2. Code-Behind Logic (C#) In the code-behind file, you can populate the control programmatically and handle selection events using the inherited properties of `ListControl`. ```csharp using System; using System.Collections.Generic; using System.Web.UI; public partial class ListControlExample : Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDepartmentData(); } } private void BindDepartmentData() { // Create a mock data source List departments = new List { new Department { DepartmentID = 101, DepartmentName = "Administration" }, new Department { DepartmentID = 102, DepartmentName = "Engineering" }, new Department { DepartmentID = 103, DepartmentName = "Marketing" } }; // Bind the list to the DropDownList control ddlDepartments.DataSource = departments; ddlDepartments.DataBind(); } protected void ddlDepartments_SelectedIndexChanged(object sender, EventArgs e) { // Accessing selection properties inherited from ListControl string selectedText = ddlDepartments.SelectedItem.Text; string selectedValue = ddlDepartments.SelectedValue; int selectedIndex = ddlDepartments.SelectedIndex; lblMessage.Text = $"Selected Index: {selectedIndex} | " + $"Department: {selectedText} (ID: {selectedValue})"; } } // Simple model class for data binding public class Department { public int DepartmentID { get; set; } public string DepartmentName { get; set; } } ``` --- ## Key Considerations * **Abstract Class**: You cannot instantiate the `ListControl` class directly. Instead, you must use one of its derived classes (e.g., `DropDownList`, `ListBox`). * **AutoPostBack Performance**: Setting `AutoPostBack="true"` causes an immediate round-trip to the server whenever a user changes their selection. Use this feature judiciously to avoid unnecessary server load and latency. * **AppendDataBoundItems**: By default, binding a new data source to a list control clears all existing items. If you want to keep static items (such as a default `"-- Select an Option --"` item) at the top of your list, set `AppendDataBoundItems="true"`. * **SelectedIndex vs. SelectedValue**: If no item is selected, `SelectedIndex` returns `-1`, and `SelectedValue` returns an empty string (`""`). When setting `SelectedValue` programmatically, if the specified value does not exist in the list, an `ArgumentOutOfRangeException` will be thrown.
← Python XmlProp Control Standard β†’