제목 : 드롭다운리스트의 다중 선택값을 콤마 구분으로 가져오기
글번호:
|
|
321
|
작성자:
|
|
레드플러스
|
작성일:
|
|
2009/10/21 오후 1:20:00
|
조회수:
|
|
6278
|
FrmDropDownListMultiSelect.aspx
====================================================
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="FrmDropDownListMultiSelect.aspx.cs" Inherits="FrmDropDownListMultiSelect" %>
<!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>
<asp:ListBox ID="DropDownList1" runat="server" SelectionMode="Multiple">
</asp:ListBox>
<hr></hr>
<asp:Button ID="Button1" runat="server" Text="다중선태값 출력"
onclick="Button1_Click"></asp:Button>
<hr></hr>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
FrmDropDownListMultiSelect.aspx.cs
====================================================
using System;
using System.Web.UI.WebControls;
public partial class FrmDropDownListMultiSelect : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10; i++)
{
//[1] Text와 Value를 DB에서 구분해서 읽어와서...
ListItem li =
new ListItem(i.ToString(), String.Format("{0}번", i));
//[2] DropDownList 컨트롤에 등록
this.DropDownList1.Items.Add(li);
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string s = "";
for (int i = 0; i < this.DropDownList1.Items.Count; i++)
{
if (this.DropDownList1.Items[i].Selected)
{
s += this.DropDownList1.Items[i].Value + " ";
}
}
s = s.Trim().Replace(" ", ",");
this.Label1.Text = s; // DB에 저장
}
}