MoniWikiASP.NET/Check Box
ID
Password
Join
U E D R S I H C RSS

좌파책의등급트위터한옥2003리눅스수업 ASP.NET/CheckBox



1 바인딩 가능 체크박스 만들기

받기를 DataSet으로 하고, 거꾸로 입력을 Custom Entities로 하는 ListView 컨트롤이 있다고 하자.
받아오는 값은 'Y/N'으로 문자열이고, 입력할 때는 bool 값으로 되어야 한다.
이때 아래와 같이 사용하면 에러가 발생한다.
<asp:CheckBox ID="HasDescChk" runat="server" Checked='<%# Bind("HasDescriptionYN") %>' />

이런 때는 간단하게 커스텀 컨트롤을 만들면 된다.
솔루션 탐색기에서 추가-ASP.NET 서버 컨트롤을 선택해 생성한 다음
코드를 아래와 같이 작성한다.


<%@ Register TagPrefix="uc" Namespace="layer.common" Assembly="layer" %>
<!-- ... -->
<uc:BindableCheckBox ID="HasDescChk" runat="server" Checked='<%# Bind("HasDescriptionYN") %>' />

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace layer.common
{
        [ToolboxData("<{0}:BindableCheckBox runat=server></{0}:BindableCheckBox>")]
        public class BindableCheckBox : CheckBox
        {
                [Bindable(true)]
                [Category("Appearance")]
                [DefaultValue("")]
                public object Bind
                {
                        get
                        {
                                //체크박스의 체크 여부를 리턴한다.
                                return (bool)ViewState["Checked"];
                        }

                        set
                        {
                                //값이 Y거나 N인 문자열을 받아 체크한다.
                                Checked = ((string) value) == "Y";
                        }
                }

                protected override void RenderContents(HtmlTextWriter output)
                {
                        output.Write(Text);
                }
        }
}

last modified 2010-07-02 14:08:58
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
1.0927 sec