ASP.NET 기초 강의실

시삽: 레드플러스 님 
게시판 이동:
 제목 : 동적으로 DropDownList 생성 및 선택된 값 가져오기
글번호: 345
작성자: 레드플러스
작성일: 2014/06/05 오전 5:33:00
조회수: 6711
파일: FrmDropDownListDynamicCreation.png (58 KB) / 전송수: 1542
FrmDropDownListDynamicCreation.png
[FrmDropDownListDynamicCreation.aspx]


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmDropDownListDynamicCreation.aspx.cs" Inherits="FrmDropDownListDynamicCreation" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>동적으로 DropDownList 생성 및 선택된 값 가져오기</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

        <hr />

        <asp:Button ID="btnGet" runat="server" Text="값 가져오기" OnClick="btnGet_Click" />

        <hr />

        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

    </div>
    </form>
</body>
</html>




[FrmDropDownListDynamicCreation.aspx.cs]

using System;
using System.Web.UI.WebControls;

public partial class FrmDropDownListDynamicCreation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        for (int i = 1; i <= 2; i++)
        {
            DropDownList ddl = new DropDownList();
            ddl.ID = String.Format("ddl{0}", i); // ddl1, ddl2
            ddl.Items.Add(new ListItem("Text1", "Value1"));
            ddl.Items.Add(new ListItem("Text2", "Value2"));
            ddl.Items.Add(new ListItem("Text3", "Value3"));
            PlaceHolder1.Controls.Add(ddl); // 동적으로 생성된 드롭다운리스트 추가
        }
    }
    protected void btnGet_Click(object sender, EventArgs e)
    {
        Label1.Text = "선택된 값: ";
        for (int i = 1; i <= 2; i++)
        {
            DropDownList ddl = PlaceHolder1.FindControl(String.Format("ddl{0}", i)) as DropDownList;
            if (ddl != null)
            {
                Label1.Text += ddl.SelectedValue + ", ";
            }
        }
    }
}

 
이전 글   다음 글 삭제 수정 답변 글쓰기 리스트


관련 아티클 리스트
  제       목 파일 작성자 작성일 조회
이전글 10. 사용자 정의 컨트롤(User Defined Control) - 레드플러스 2003-11-15 5663
  9. 리스트 컨트롤(List Control) - 레드플러스 2003-11-15 5638
  DropDownList 컨트롤 - 레드플러스 2004-06-16 7163
현재글 동적으로 DropDownList 생성 및 선택된 값 가져오기 FrmDropDownListDynamicCreation.png(58 KB) 레드플러스 2014-06-05 6711
  DropDownList 컨트롤에 년월일 바인딩하기 FrmDropDownList_Year_Month_Day_Form.png(10 KB) 레드플러스 2014-09-23 3945
  ListBox 컨트롤 - 레드플러스 2004-06-16 6014
다음글 8. 리치 컨트롤(Rich Control) - 레드플러스 2003-11-15 5742
 
손님 사용자 Anonymous (손님)
로그인 Home