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); } } }
powered by Moniwiki | themed by clockoon
last modified 2010-07-02 05:08:58
Processing time 0.5792 sec