ASP.NET 기초 강의실

시삽: 레드플러스 님 
게시판 이동:
 제목 : DropDownList 컨트롤에 년월일 바인딩하기
글번호: 350
작성자: 레드플러스
작성일: 2014/09/23 오후 2:14:00
조회수: 3903
파일: FrmDropDownList_Year_Month_Day_Form.png (10 KB) / 전송수: 1350
FrmDropDownList_Year_Month_Day_Form.png

 

(1) 아래 그림과 같이 드롭다운리스트를 사용하여 년월일을 동적으로 바인딩하려고 한다. 

특정 년도와 월을 선택하면 해당 년월에 해당하는 날짜수를 자동으로 계산해서 [일]수로 표시해야 한다. 

(2) FrmDropDownList_Year_Month_Day.aspx 소스

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FrmDropDownList_Year_Month_Day.aspx.cs" Inherits="WebStandardControl.FrmDropDownList_Year_Month_Day" %>

<!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>드롭다운리스트에 년월일 바인딩하기</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:DropDownList ID="lstYear" runat="server" OnSelectedIndexChanged="lstYear_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>년
        <asp:DropDownList ID="lstMonth" runat="server" OnSelectedIndexChanged="lstMonth_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>월
        <asp:DropDownList ID="lstDay" runat="server"></asp:DropDownList>일

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

 

(3) FrmDropDownList_Year_Month_Day.aspx.cs 소스

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

namespace WebStandardControl
{
    public partial class FrmDropDownList_Year_Month_Day : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DisplayYear();
                DisplayMonth();
                DisplayDay(31);
            }
        }

        private void DisplayDay(int maxDay)
        {
            this.lstDay.Items.Clear(); 
            for (int i = 1; i <= maxDay; i++)
            {
                this.lstDay.Items.Add(new ListItem(i.ToString()));
            }
        }

        private void DisplayMonth()
        {
            for (int i = 1; i <= 12; i++)
            {
                this.lstMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }
        }

        private void DisplayYear()
        {
            // 현재년도부터 100년간의 날짜를 바인딩 : 6세 이상부터 회원 가입 가능
            for (int i = DateTime.Now.Year - 6; i > 1900; i--)
            {
                this.lstYear.Items.Add(new ListItem(i.ToString()));
            }
        }

        protected void lstYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetDaysInMonth();
        }

        private void GetDaysInMonth()
        {
            int day = DateTime.DaysInMonth(Convert.ToInt32(lstYear.SelectedValue), Convert.ToInt32(lstMonth.SelectedValue));
            DisplayDay(day);
        }

        protected void lstMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetDaysInMonth();
        }
    }
}

 

끝.

 

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


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