ListItem.Enabled 属性

定义

获取或设置指示是否启用了列表项的值。

public:
 property bool Enabled { bool get(); void set(bool value); };
public bool Enabled { get; set; }
member this.Enabled : bool with get, set
Public Property Enabled As Boolean

属性值

如果启用了列表项,则为 true;否则为 false。 默认值为 true

示例

以下示例演示如何使用 Enabled 属性以编程方式禁用控件中的 RadioButtonList 列表项。 第一个问题要求用户选择与用户职业对应的单选按钮。 如果用户指示他们不是开发人员,则会禁用第二个单选按钮列表中的列表项。 第二个问题要求用户选择主要编程语言。 此问题与非开发人员用户无关。

注意

下面的代码示例使用单文件代码模型,如果直接复制到代码隐藏文件中,可能无法正常工作。 必须将此代码示例复制到扩展名为 .aspx 的空文本文件中。 有关Web Forms代码模型的详细信息,请参阅 ASP.NET Web Forms页代码模型

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ListItem.Enabled Property Example</title>
<script runat="server">         
  
    protected void  Index_Changed(object sender, EventArgs e)
    {

        // if the user is not a developer, do not
        // ask the user to select a programming language.
        if (RadioButtonList1.SelectedIndex == 2)
        {   
            // Clear any previously selected list 
            // items in the second question.
            RadioButtonList2.SelectedIndex = -1;

            // Disable all the list items in the second question.
            for (int i = 0; i < RadioButtonList2.Items.Count; i++)
            {
                RadioButtonList2.Items[i].Enabled = false;
            }
        }          
        else
        // Enable all the list items in the second question.
            for (int i = 0; i < RadioButtonList2.Items.Count; i++)
        {
            RadioButtonList2.Items[i].Enabled = true;
        }
    }
</script>
  </head>

  <body>
    <form id="form1" runat="server">
        
      <h3>ListItem.Enabled Property Example</h3>
      
      Select your occupation:
      <asp:radiobuttonlist id="RadioButtonList1"
        autopostback="true"
        onselectedindexchanged="Index_Changed" 
    runat="server">             
      <asp:ListItem>Web developer</asp:ListItem>
      <asp:ListItem>Windows developer</asp:ListItem>
      <asp:ListItem>Occupation other than developer</asp:ListItem>
        </asp:radiobuttonlist>
          
    <br /><br />
          
    Select your primary programming language:
        <asp:radiobuttonlist id="RadioButtonList2" 
      runat="server">             
       <asp:ListItem>Visual Basic .NET</asp:ListItem>
       <asp:ListItem>C#</asp:ListItem>
       <asp:ListItem>C++</asp:ListItem>
       <asp:ListItem>Other</asp:ListItem>
    </asp:radiobuttonlist> 
                  
    </form>      
  </body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>ListItem.Enabled Property Example</title>
<script runat="server">         
  
      Sub Index_Changed(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim i As Integer

        ' If the user is not a developer, do not
        ' ask the user to select a programming language.
        If radiobuttonlist1.SelectedIndex = 2 Then
          ' Clear any previously selected list 
          ' items in the second question.
          Radiobuttonlist2.SelectedIndex = -1

          ' Disable all the list items in the second question.
          For i = 0 To Radiobuttonlist2.Items.Count - 1
            Radiobuttonlist2.Items(i).Enabled = False
          Next
          
        Else
          ' Enable all the list items in the second question.
          For i = 0 To Radiobuttonlist2.Items.Count - 1
            Radiobuttonlist2.Items(i).Enabled = True
          Next i

        End If
      End Sub
    
      </script>
  </head>

  <body>
    <form id="form1" runat="server">
        
      <h3>ListItem.Enabled Property Example</h3>
      
      Select your occupation:
      <asp:radiobuttonlist id="RadioButtonList1"
        autopostback="true"
        onselectedindexchanged="Index_Changed" 
    runat="server">             
      <asp:ListItem>Web developer</asp:ListItem>
      <asp:ListItem>Windows developer</asp:ListItem>
      <asp:ListItem>Occupation other than developer</asp:ListItem>
        </asp:radiobuttonlist>
          
    <br /><br />
          
    Select your primary programming language:
        <asp:radiobuttonlist id="RadioButtonList2" 
      runat="server">             
       <asp:ListItem>Visual Basic .NET</asp:ListItem>
       <asp:ListItem>C#</asp:ListItem>
       <asp:ListItem>C++</asp:ListItem>
       <asp:ListItem>Other</asp:ListItem>
    </asp:radiobuttonlist> 
                  
    </form>      
  </body>
</html>

注解

属性 Enabled 允许你指定 ListItem 是启用或禁用控件。 ListItem禁用的控件灰显,以指示无法选择它。 使用此属性可禁用ListItem控件或CheckBoxList控件中的RadioButtonList控件。

注意

不能使用此属性禁用ListItem控件或ListBox控件中的DropDownList控件。

适用于

另请参阅