CheckBox Control

Reference: ASP.NET 2.0 웹 프로젝트와 실전 프로그래밍
                MSDN

==================================
CheckBox 컨트롤은 항목을 체크했는지 체크하지 않았는지를 나타내주는 단순한 컨트롤입니다.
CheckBox를 사용하여 true/false 또는 yes/no와 같은 옵션을 선택할 수 있습니다. CheckBox 컨트롤에는 이미지, 텍스트 또는 이 둘을 모두 표시할 수 있습니다.
CheckBoxRadioButton 컨트롤의 기능은 옵션 목록에서 선택할 수 있다는 점에서 유사합니다. 그러나 CheckBox 컨트롤에서는 여러 옵션을 동시에 선택할 수 있지만 RadioButton 컨트롤에서는 동시에 선택할 수 없습니다.
ASP.NET 2.0 에서 CheckBox 컨트롤에는 InputAttributes 와 LabelAttributes 속성이 추가 되였습니다. CheckBox 컨트롤이 렌더링 되면 보통 두 종류의 태그가 생성 됩니다. 예를 들어 아래와 같이 CheckBox 컨트롤을 추가 했다고 하면

 

다음과 같은 두 종류의 태그가 렌더링 됩니다.

<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox" />

<input id="CheckBox1" type="checkbox" name="CheckBox1" />
<label for="CheckBox1">CheckBox</label> 
InputAttributes와 LabelAttributes 속성은 모두 AttributeCollection 형식의 컬렉션인데, 여기에 저장된 데이터는 렌더링된 태그 안에 특성으로 추가 됩니다. 즉, InputAttributes에 포함된 요소는 <input type="checkbox"> 태그의 특성으로 사용되고, LabelAttributes의 요소들은 <label>태그의 특성이 됩니다.

InputAttributes와 LabelAttributes 속성은 그 특성상 디자인 타임에는 접근할 수 없고 프로그램 방식으로만 사용 가능합니다.

아래 DEMO를 보시면 인츰 이해가 될 것입니다.
========================================================================================== 
Result:

CheckBoxTest.aspx
==============

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckBox1.InputAttributes.Add("onmouseover", "this.style.backgroundColor = 'red'");
        CheckBox1.InputAttributes.Add("onmouseout", "this.style.backgroundColor = 'white'");

        CheckBox1.LabelAttributes.Add("onmouseover", "this.style.backgroundColor = 'blue'");
        CheckBox1.LabelAttributes.Add("onmouseout", "this.style.backgroundColor = 'white'");
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox" />
    </div>
    </form>
</body>
</html>

posted @ 2009-02-01 16:59  OOK  阅读(243)  评论(0编辑  收藏  举报