ListControl.SelectedIndex Propriété
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Obtient ou définit l’index ordinal le plus bas des éléments sélectionnés dans la liste.
public:
virtual property int SelectedIndex { int get(); void set(int value); };
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
public virtual int SelectedIndex { get; set; }
[System.ComponentModel.Bindable(true)]
[System.ComponentModel.Browsable(false)]
[System.Web.UI.Themeable(false)]
public virtual int SelectedIndex { get; set; }
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.Browsable(false)>]
member this.SelectedIndex : int with get, set
[<System.ComponentModel.Bindable(true)>]
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.Themeable(false)>]
member this.SelectedIndex : int with get, set
Public Overridable Property SelectedIndex As Integer
Valeur de propriété
Index ordinal le plus bas des éléments sélectionnés dans la liste. La valeur par défaut est -1, ce qui indique que rien n’est sélectionné.
- Attributs
Exceptions
L’index a été défini sur moins de -1, ou supérieur ou égal au nombre d’éléments de la liste au moment du rendu de la liste.
Exemples
L’exemple suivant montre comment utiliser SelectedIndex pour déterminer l’index le plus bas des éléments sélectionnés dans le CheckBoxList.
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!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" >
<script language="c#" runat="server">
ICollection CreateDataSource() {
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("DateTimeValue", typeof(DateTime)));
dt.Columns.Add(new DataColumn("BoolValue", typeof(bool)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
for (int i = 0; i < 9; i++) {
dr = dt.NewRow();
dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = DateTime.Now;
dr[3] = (i % 2 != 0) ? true : false;
dr[4] = 1.23 * (i+1);
dt.Rows.Add(dr);
}
DataView dv = new DataView(dt);
return dv;
}
void Page_Load(Object sender, EventArgs e) {
if (!IsPostBack) {
CheckBoxList1.DataSource = CreateDataSource();
CheckBoxList1.DataTextField="StringValue";
CheckBoxList1.DataValueField="CurrencyValue";
CheckBoxList1.DataBind();
}
}
void Index_Changed(Object sender, EventArgs e) {
Label1.Text = "The index of the first item selected is: " +
CheckBoxList1.SelectedIndex.ToString();
}
</script>
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList id="CheckBoxList1"
OnSelectedIndexChanged="Index_Changed"
AutoPostBack="true"
runat="server"/>
<br />
<asp:Label id="Label1" runat="server"/>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Data" %>
<!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" >
<script language="vb" runat="server">
Function CreateDataSource() As ICollection
Dim dt As New DataTable()
Dim dr As DataRow
dt.Columns.Add(New DataColumn("IntegerValue", GetType(Int32)))
dt.Columns.Add(New DataColumn("StringValue", GetType(String)))
dt.Columns.Add(New DataColumn("DateTimeValue", GetType(DateTime)))
dt.Columns.Add(New DataColumn("BoolValue", GetType(Boolean)))
dt.Columns.Add(New DataColumn("CurrencyValue", GetType(Double)))
Dim i As Integer
For i = 0 To 8
dr = dt.NewRow()
dr(0) = i
dr(1) = "Item " + i.ToString()
dr(2) = DateTime.Now
If (i Mod 2) <> 0 Then
dr(3) = True
Else
dr(3) = False
End If
dr(4) = 1.23 *(i + 1)
dt.Rows.Add(dr)
Next i
Dim dv As New DataView(dt)
Return dv
End Function
Sub Page_Load(sender As Object, e As EventArgs)
If Not IsPostBack Then
CheckBoxList1.DataSource = CreateDataSource()
CheckBoxList1.DataTextField = "StringValue"
CheckBoxList1.DataValueField = "CurrencyValue"
CheckBoxList1.DataBind()
End If
End Sub
Sub Index_Changed(sender As Object, e As EventArgs)
Label1.Text = "The index of the first item selected is: " & _
CheckBoxList1.SelectedIndex.ToString()
End Sub
</script>
<head runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBoxList id="CheckBoxList1"
OnSelectedIndexChanged="Index_Changed"
AutoPostBack="true"
runat="server"/>
<br />
<asp:Label id="Label1" runat="server"/>
</form>
</body>
</html>
Remarques
Utilisez cette propriété pour déterminer l’index de l’élément actuellement sélectionné dans la liste si le contrôle de liste n’autorise qu’une seule sélection. Si le contrôle de liste prend en charge plusieurs sélections, utilisez cette propriété pour déterminer l’index le plus bas des éléments sélectionnés.
La valeur de la propriété n’est pas spécifiquement enregistrée lors de la SelectedIndex publication. Elle est restaurée à l’aide de la publication à l’aide de la SelectedValue propriété. Par exemple, si la SelectedValue propriété est 4, lors de la publication, la SelectedIndex propriété est définie sur l’index de l’objet ListItem dont la propriété est Value 4. Si plusieurs ListItem objets ont une Value propriété de 4, le premier élément de la Items collection est sélectionné.
Cette propriété ne peut pas être définie par des thèmes ou des thèmes de feuille de style. Pour plus d’informations, consultez ThemeableAttribute et ASP.NET Thèmes et peaux.