ASP.NET Tutorial/ASP.net Controls/DropDownList
Содержание
Dynamically generating a DropDownList control from an array (C#)
<%@ Page Language="C#" %>
<script runat="server">
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string[] CarArray = new string[4] {"Ford", "Honda", "BMW", "Dodge"};
string[] AirplaneArray = new string[3] {"Boeing 777", "Boeing 747","Boeing 737"};
string[] TrainArray = new string[3] {"Bullet Train", "Amtrack", "Tram"};
if (DropDownList1.SelectedValue == "Car") {
DropDownList2.DataSource = CarArray; }
else if (DropDownList1.SelectedValue == "Airplane") {
DropDownList2.DataSource = AirplaneArray; }
else {
DropDownList2.DataSource = TrainArray;
}
DropDownList2.DataBind();
DropDownList2.Visible = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("You selected <b>" +
DropDownList1.SelectedValue.ToString() + ": " +
DropDownList2.SelectedValue.ToString() + "</b>");
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>DropDownList Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Select transportation type:<br />
<asp:DropDownList ID="DropDownList1"
Runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem>Select an Item</asp:ListItem>
<asp:ListItem>Car</asp:ListItem>
<asp:ListItem>Airplane</asp:ListItem>
<asp:ListItem>Train</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" Runat="server" Visible="false">
</asp:DropDownList>
<asp:Button ID="Button1" Runat="server" Text="Select Options"
OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Dynamically generating a DropDownList control from an array (VB)
<%@ Page Language="VB" %>
<script runat="server">
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim CarArray() As String = {"Ford", "Honda", "BMW", "Dodge"}
Dim AirplaneArray() As String = {"Boeing 777", "Boeing 747", "Boeing 737"}
Dim TrainArray() As String = {"Bullet Train", "Amtrack", "Tram"}
If DropDownList1.SelectedValue = "Car" Then
DropDownList2.DataSource = CarArray
ElseIf DropDownList1.SelectedValue = "Airplane" Then
DropDownList2.DataSource = AirplaneArray
Else
DropDownList2.DataSource = TrainArray
End If
DropDownList2.DataBind()
DropDownList2.Visible = True
End Sub
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs)
Response.Write("You selected <b>" & _
DropDownList1.SelectedValue.ToString() & ": " & _
DropDownList2.SelectedValue.ToString() & "</b>")
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>DropDownList Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
Select transportation type:<br />
<asp:DropDownList ID="DropDownList1" Runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem>Select an Item</asp:ListItem>
<asp:ListItem>Car</asp:ListItem>
<asp:ListItem>Airplane</asp:ListItem>
<asp:ListItem>Train</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" Runat="server" Visible="false">
</asp:DropDownList>
<asp:Button ID="Button1" Runat="server" Text="Select Options"
OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
Get selected index from asp:dropdownlist
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="MyPage" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Style="z-index: 100; left: 151px; position: absolute;
top: 120px" Text="What is the date"></asp:Label>
<asp:Label ID="result" runat="server" Height="25px" Style="z-index: 101; left: 159px;
position: absolute; top: 242px" Text="Label" Width="324px"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Height="48px"
Style="z-index: 102; left: 274px; position: absolute; top: 118px" Width="243px">
</asp:DropDownList>
<asp:Button ID="Button1" runat="server" Style="z-index: 104; left: 393px; position: absolute;
top: 168px" Text="OK" />
</div>
</form>
</body>
</html>
File: Default.aspx.vb
Partial Class MyPage
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Select Case Me.DropDownList1.SelectedIndex
Case 0
Me.result.Text = "A"
Case 1
Me.result.Text = "B"
Case 2
Me.result.Text = "C"
Case 3
Me.result.Text = "D"
Case 4
Me.result.Text = "E"
Case 5
Me.result.Text = "F"
Case 6
Me.result.Text = "G"
Case 7
Me.result.Text = "H"
Case 8
Me.result.Text = "I"
Case 9
Me.result.Text = "J"
Case 10
Me.result.Text = "K"
Case 11
Me.result.Text = "L"
End Select
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
Me.DropDownList1.Items.Add("A")
Me.DropDownList1.Items.Add("B")
Me.DropDownList1.Items.Add("C")
Me.DropDownList1.Items.Add("D")
Me.DropDownList1.Items.Add("E")
Me.DropDownList1.Items.Add("F")
Me.DropDownList1.Items.Add("G")
Me.DropDownList1.Items.Add("H")
Me.DropDownList1.Items.Add("I")
Me.DropDownList1.Items.Add("J")
Me.DropDownList1.Items.Add("K")
Me.DropDownList1.Items.Add("L")
End If
End Sub
End Class
Get selected item in DropDownList
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="DropDownList" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
What is your favorite ice cream flavor?
<asp:DropDownList ID="flavors" runat="server">
<asp:ListItem>A</asp:ListItem>
<asp:ListItem>B</asp:ListItem>
<asp:ListItem>C</asp:ListItem>
</asp:DropDownList>
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Click Me" />
<br />
<br />
<asp:Label ID="results" runat="server"></asp:Label></div>
</form>
</body>
</html>
File: Default.aspx.vb
Partial Class DropDownList
Inherits System.Web.UI.Page
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
results.Text = "You like " & flavors.SelectedItem.Text
End Sub
End Class