ListControl 类

用作定义所有列表类型控件通用的属性、方法和事件的抽象基类。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
<ControlValuePropertyAttribute("SelectedValue")> _
Public MustInherit Class ListControl
    Inherits DataBoundControl
    Implements IEditableTextControl, ITextControl
用法
Dim instance As ListControl
[ControlValuePropertyAttribute("SelectedValue")] 
public abstract class ListControl : DataBoundControl, IEditableTextControl, ITextControl
[ControlValuePropertyAttribute(L"SelectedValue")] 
public ref class ListControl abstract : public DataBoundControl, IEditableTextControl, ITextControl
/** @attribute ControlValuePropertyAttribute("SelectedValue") */ 
public abstract class ListControl extends DataBoundControl implements IEditableTextControl, ITextControl
ControlValuePropertyAttribute("SelectedValue") 
public abstract class ListControl extends DataBoundControl implements IEditableTextControl, ITextControl

备注

不能直接创建 ListControl 抽象类的实例。相反,此类由其他类(如 CheckBoxListDropDownListListBoxRadioButtonList 类)继承以提供通用的基本功能。

ListControl 类的属性允许您指定用来填充列表控件的数据源。使用 DataSource 属性指定要绑定到列表控件的数据源。如果数据源包含多个表,请使用 DataMember 属性指定要使用的表。通过分别设置 DataTextFieldDataValueField 属性,可以将数据源中的不同字段绑定到列表控件项的 ListItem.TextListItem.Value 属性。通过设置 DataTextFormatString 属性,可以设定列表控件中每一项的显示文本的格式。

列表控件中显示的所有项都保存在 Items 集合中。可以使用 SelectedIndex 属性,以编程方式指定或确定列表控件中选定项的索引。使用 SelectedItem 属性,可以访问选定项的属性。

ListControl 类提供了 SelectedIndexChanged 事件,在信息发往服务器之间,如果列表控件中的选定项发生变化,会引发该事件。这使您可以为此事件提供自定义处理程序。有关处理事件的更多信息,请参见 使用事件

ListControl 类实现 IEditableTextControl 接口,以便派生的列表类型的控件可在其他控件的模板模式下使用。

示例

下面的代码示例演示如何选择 ListBox 控件中的项。如果在文本框中找到相应项,则选定该项,同时显示一条消息,指出选定项的名称。如果未找到相应项,则不选择任何项,同时显示一个消息,指出未找到该项。

<%@ 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>
<%@ 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>

继承层次结构

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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

ListControl 成员
System.Web.UI.WebControls 命名空间
CheckBoxList 类
DropDownList 类
ListBox 类
RadioButtonList
ListItem