Properti SelectedIndex ASP.NET
Definisi dan Penggunaan
Properti SelectedIndex digunakan untuk mendapatkan atau mengatur indeks item yang dipilih di DropDownList.
Contoh
Contoh di bawah ini menampilkan teks item yang dipilih:
<script runat="Server"> Sub GetName(obj As Object, e As EventArgs) lbl.Text = ddList.SelectedItem.Text End Sub </script> <form runat="Server"> Pilih nama dari daftar: <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="Select" OnClick="GetName" Runat="Server" /> <br /> Nama adalah: <asp:Label id="lbl" Runat="Server"/> </form>
Contoh
- Ambil teks dari salah satu item DropDownList