DropDownList.SelectedIndex 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 DropDownList 控件中的选定项的索引。
public:
virtual property int SelectedIndex { int get(); void set(int value); };
public override int SelectedIndex { get; set; }
member this.SelectedIndex : int with get, set
Public Overrides Property SelectedIndex As Integer
属性值
DropDownList 控件中的选定项的索引。 默认值为 0,该值选择列表中的第一项。
示例
下面的代码示例演示如何使用 SelectedIndex 属性来确定控件中的 DropDownList 选定项。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!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 runat="server" >
void Selection_Change(Object sender, EventArgs e)
{
// Set the title format based on the value selected from the
// DropDownList control. The SelectedIndex property can be used
// to determine an enumeration value. Simply cast the value to
// the appropriate enumeration type. Note that the value might
// need to be adjusted if the enumeration type is not zero-based.
Calendar1.TitleFormat = (TitleFormat)ModeList.SelectedIndex;
}
</script>
<head runat="server">
<title> DropDownList SelectedIndex Example </title>
</head>
<body>
<form id="form1" runat="server">
<h3> DropDownList SelectedIndex Example </h3>
Choose the title format.
<br /><br />
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br /><br />
<table cellpadding="5">
<tr>
<td>
Format:
</td>
</tr>
<tr>
<td>
<asp:DropDownList id="ModeList"
AutoPostBack="True"
OnSelectedIndexChanged="Selection_Change"
runat="server">
<asp:ListItem> Month </asp:ListItem>
<asp:ListItem Selected="True"> MonthYear </asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!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 runat="server" >
Sub Selection_Change(sender As Object, e As EventArgs)
' Set the title format based on the value selected from the
' DropDownList control. The SelectedIndex property can be used
' to determine an enumeration value. Simply cast the value to
' the appropriate enumeration type. Note that the value might
' need to be adjusted if the enumeration type is not zero-based.
Calendar1.TitleFormat = CType(ModeList.SelectedIndex, TitleFormat)
End Sub
</script>
<head runat="server">
<title> DropDownList SelectedIndex Example </title>
</head>
<body>
<form id="form1" runat="server">
<h3> DropDownList SelectedIndex Example </h3>
Choose the title format.
<br /><br />
<asp:Calendar id="Calendar1"
ShowGridLines="True"
ShowTitle="True"
runat="server"/>
<br /><br />
<table cellpadding="5">
<tr>
<td>
Format:
</td>
</tr>
<tr>
<td>
<asp:DropDownList id="ModeList"
AutoPostBack="True"
OnSelectedIndexChanged="Selection_Change"
runat="server">
<asp:ListItem> Month </asp:ListItem>
<asp:ListItem Selected="True"> MonthYear </asp:ListItem>
</asp:DropDownList>
</td>
</tr>
</table>
</form>
</body>
</html>
注解
SelectedIndex使用属性以编程方式指定或确定控件中DropDownList所选项的索引。 控件中 DropDownList 始终选择项。 不能同时清除列表中的每一个项目的选择。
备注
控件中 DropDownList 项的索引从零开始。