Atrybut SelectedIndex w ASP.NET

Definicja i użycie

Atrybut SelectedIndex służy do uzyskania lub ustawienia indeksu wybranego elementu w DropDownList.

Przykład

Poniższy przykład wyświetla tekst wybranego elementu:

<script runat="Server">
Podsekcja GetName(obj As Object, e As EventArgs)
  lbl.Text = ddList.SelectedItem.Text
Koniec Podsekcji
</script>
<form runat="Server">
Wybierz imię z listy:
<asp:DropDownList id="ddList" runat="Server">
  <asp:ListItem Text="Peter" />
  <asp:ListItem Text="Lois" />
  <asp:ListItem Text="Cleveland" />
  <asp:ListItem Text="Quagmire" />
  <asp:ListItem Text="Joe" />
</asp:DropDownList>
<asp:Button Text="Wybierz"
OnClick="GetName" Runat="Server" />
<br />
Imię jest:
<asp:Label id="lbl" Runat="Server"/>
</form>

Przykład

Pobierz tekst z jednego elementu DropDownList