ASP.NET DropDownList, List 사용 예제
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>
Ex30_DropDownList.aspx</h2>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="one">하나</asp:ListItem>
<asp:ListItem Value="two">둘</asp:ListItem>
<asp:ListItem Value="three">셋</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<br />
<asp:DropDownList ID="DropDownList2" runat="server"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<br />
<asp:DropDownList ID="DropDownList3" runat="server" EnableViewState="False"
onselectedindexchanged="DropDownList3_SelectedIndexChanged">
</asp:DropDownList>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"
Width="128px" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Ex30_DropDownList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//두번째 DropDownList 초기화
//컨트롤의 초기화 = !IsPost
if (!IsPostBack)
{
DropDownList2.Items.Add(new ListItem("하나", "one"));
DropDownList2.Items.Add(new ListItem("둘", "two"));
DropDownList2.Items.Add(new ListItem("셋", "three"));
}
DropDownList3.Items.Add(new ListItem("하나", "one"));
DropDownList3.Items.Add(new ListItem("둘", "two"));
DropDownList3.Items.Add(new ListItem("셋", "three"));
}
protected void Button1_Click(object sender, EventArgs e)
{
string 한글 = DropDownList1.SelectedItem.Text;
string 영어 = DropDownList1.SelectedItem.Value;
Label1.Text = 한글 + " : " + 영어;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string 한글 = DropDownList1.SelectedItem.Text;
string 영어 = DropDownList1.SelectedItem.Value;
Label1.Text = 한글 + " : " + 영어;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
string 한글 = DropDownList2.SelectedItem.Text;
string 영어 = DropDownList2.SelectedItem.Value;
Label2.Text = 한글 + " : " + 영어;
}
protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
string 한글 = DropDownList3.SelectedItem.Text;
string 영어 = DropDownList3.SelectedItem.Value;
Label3.Text = 한글 + " : " + 영어;
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ex31_List.aspx.cs" Inherits="Ex31_List" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
Ex31_List.aspx<br />
<br />
<br />
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="one">하나</asp:ListItem>
<asp:ListItem Value="two">둘</asp:ListItem>
<asp:ListItem Value="three">셋</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:RadioButtonList ID="RadioButtonList2" runat="server"
RepeatDirection="Horizontal">
</asp:RadioButtonList>
<br />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" />
<br />
<br />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple">
<asp:ListItem>하나</asp:ListItem>
<asp:ListItem>둘</asp:ListItem>
<asp:ListItem>셋</asp:ListItem>
</asp:ListBox>
<br />
<br />
<asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Button" />
<br />
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Label"></asp:Label>
<br />
<br />
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
</asp:CheckBoxList>
<br />
<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Button" />
<br />
<br />
<asp:Label ID="Label4" runat="server" Text="Label"></asp:Label>
<br />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Ex31_List : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//초기화
if (!IsPostBack)
{
for (int i = 0; i < 20; i+=2)
{
//두개다 문자열이라 투스트링을 붙여주도록 한다.
RadioButtonList2.Items.Add(new ListItem("아이템" + i, i.ToString()));
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = RadioButtonList1.SelectedItem.Text;
}
protected void Button2_Click(object sender, EventArgs e)
{
Label2.Text = RadioButtonList2.SelectedItem.Text;
}
protected void Button3_Click(object sender, EventArgs e)
{
//Label3.Text = ListBox1.SelectedItem.Text;
//다중 선택 가져오기
//직접하나씩가져와서 셀렉티드 아이템이 트룽니지 펄스인지
//제공하는것이 없기때문에 직접접근해야함.
for (int i = 0; i < ListBox1.Items.Count; i++)
{
Label3.Text += ListBox1.Items[i].Text + "<br />";
CheckBoxList1.Items.Add(new ListItem("항목" + i, i.ToString()));
}
}
protected void Button4_Click(object sender, EventArgs e)
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
if (CheckBoxList1.Items[i].Selected)
{
Label3.Text += CheckBoxList1.Items[i].Text + "<br />";
}
}
}
}