ListItem.Enabled Proprietà

Definizione

Ottiene o imposta un valore che indica se l'elemento elenco è abilitato.

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

Valore della proprietà

true se l'elemento elenco è abilitato; in caso contrario, false. Il valore predefinito è true.

Esempio

Nell'esempio seguente viene illustrato come utilizzare la Enabled proprietà per disabilitare gli elementi di elenco a livello di codice in un RadioButtonList controllo . La prima domanda chiede all'utente di selezionare il pulsante di opzione corrispondente all'occupazione dell'utente. Se l'utente indica che non è uno sviluppatore, le voci dell'elenco nel secondo elenco di pulsanti di opzione sono disabilitate. La seconda domanda chiede all'utente di selezionare un linguaggio di programmazione primario. Questa domanda non è rilevante per un utente che non è uno sviluppatore.

Nota

L'esempio di codice seguente usa il modello di codice a file singolo e potrebbe non funzionare correttamente se copiato direttamente in un file code-behind. Questo esempio di codice deve essere copiato in un file di testo vuoto con estensione aspx. Per altre informazioni sul modello di codice Web Forms, vedere Web Forms ASP.NET Page Code Model.

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

Commenti

La Enabled proprietà consente di specificare se un ListItem controllo è abilitato o disabilitato. Un ListItem controllo disabilitato è disattivato per indicare che non può essere selezionato. Utilizzare questa proprietà per disabilitare un ListItem controllo in un RadioButtonList controllo o in un CheckBoxList controllo .

Nota

Non è possibile utilizzare questa proprietà per disabilitare un ListItem controllo in un controllo o ListBox in un DropDownList controllo.

Si applica a

Vedi anche