Export (0) Print
Expand All

ListControl Class

Serves as the abstract base class that defines the properties, methods, and events common for all list-type controls.

For a list of all members of this type, see ListControl Members.

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.WebControl
         System.Web.UI.WebControls.ListControl
            System.Web.UI.WebControls.CheckBoxList
            System.Web.UI.WebControls.DropDownList
            System.Web.UI.WebControls.ListBox
            System.Web.UI.WebControls.RadioButtonList

[Visual Basic]
MustInherit Public Class ListControl
   Inherits WebControl
[C#]
public abstract class ListControl : WebControl
[C++]
public __gc __abstract class ListControl : public WebControl
[JScript]
public abstract class ListControl extends WebControl

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The ListControl abstract class cannot be instantiated directly. Instead, this class is inherited by other classes, such as the CheckBoxList, DropDownList, ListBox, and RadioButtonList classes, to provide common basic functionality.

The properties of the ListControl class allow you to specify the source of the data to populate the list control. Use the DataSource property to specify the data source to bind to the list control. If the data source contains more than one table, use the DataMember property to specify the table to use. You can bind different fields in the data source to the ListItem.Text and ListItem.Value properties of the items in the list control by setting the DataTextField and DataValueField properties, respectively. The text displayed for each item in the list control can by formatted by setting the DataTextFormatString property.

All items displayed in the list control are stored in the Items collection. You can programmatically specify or determine the index of selected item in the list control by using the SelectedIndex property. The properties of the selected item can be accessed by using the SelectedItem property.

The ListControl class provides the SelectedIndexChanged event, which is raised when the selection in the list control changes between posts to the server. This allows you to provide a custom handler for this event. For more information about handling events, see Consuming Events.

Example

[Visual Basic] 
<!-- The following example demonstrates how to select items in a TextBox. 
If the item is found in the text box, the item is selected and a message is 
displayed stating the name of the selected item. If the item is not found, 
no item is selected and a message is displayed stating that the item was not 
found. -->

<%@ Page Language="VB" AutoEventWireup="True" %>

<html> 

<head>

   <script runat="server">

      Sub Button_Click(sender As Object, e As EventArgs)

         ' Perform this operation in a try-catch block in case the item is not found.
         Try
   
            List.SelectedValue = ItemTextBox.Text
            MessageLabel.Text = "You selected " & List.SelectedValue + "."
        
         Catch ex As Exception
     
            List.SelectedValue = Nothing         
            MessageLabel.Text = "Item not found in ListBox control."
     
         End Try
             
      End Sub

   </script>

</head>

<body>

   <form runat="server">

      <h3> ListControl SelectedValue Example </h3>
 
      <asp:ListBox ID="List"
           runat="server">

         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>

      </asp:ListBox>

      <hr>

      Enter the value of the item to select: <br>
      <asp:TextBox ID="ItemTextBox"
           MaxLength="6"
           Text="Item 1"
           runat="server"/>

      &nbsp;&nbsp;

      <asp:Button ID="SelectButton"
           Text="Select Item"
           OnClick="Button_Click"
           runat="server"/>

      <br><br>

      <asp:Label ID="MessageLabel"
           runat="server"/>     

   </form>

</body>
</html>

[C#] 
<!-- The following example demonstrates how to select items in a TextBox. 
If the item is found in the text box, the item is selected and a message is 
displayed stating the name of the selected item. If the item is not found, 
no item is selected and a message is displayed stating that the item was not 
found. -->

<%@ Page Language="C#" AutoEventWireup="True" %>

<html> 

<head>

   <script runat="server">

      void Button_Click(Object sender, EventArgs e)
      {

         // Perform this operation in a try-catch block in case the item is not found.
         try
         {
            List.SelectedValue = ItemTextBox.Text;
            MessageLabel.Text = "You selected " + List.SelectedValue + ".";
         }
         catch (Exception ex)
         {
            List.SelectedValue = null;
            MessageLabel.Text = "Item not found in ListBox control.";
         }
             
      }

   </script>

</head>

<body>

   <form runat="server">

      <h3> ListControl SelectedValue Example </h3>
 
      <asp:ListBox ID="List"
           runat="server">

         <asp:ListItem>Item 1</asp:ListItem>
         <asp:ListItem>Item 2</asp:ListItem>
         <asp:ListItem>Item 3</asp:ListItem>
         <asp:ListItem>Item 4</asp:ListItem>

      </asp:ListBox>

      <hr>

      Enter the value of the item to select: <br>
      <asp:TextBox ID="ItemTextBox"
           MaxLength="6"
           Text="Item 1"
           runat="server"/>

      &nbsp;&nbsp;

      <asp:Button ID="SelectButton"
           Text="Select Item"
           OnClick="Button_Click"
           runat="server"/>

      <br><br>

      <asp:Label ID="MessageLabel"
           runat="server"/>     

   </form>

</body>
</html>

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.WebControls

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

ListControl Members | System.Web.UI.WebControls Namespace | CheckBoxList | DropDownList | ListBox | RadioButtonList | ListItem

Show:
© 2015 Microsoft