■ ASP.NET 표준 컨트롤을 사용하는 방법을 보여준다.
▶ MainPage.aspx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="TestProject.MainPage" %> <!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>ASP.NET 표준 컨트롤 사용하기</title> </head> <body> <form id="form" runat="server"> <div> <table style="width: 500px"> <tr> <td style="width:60px">이용 약관</td> <td style="width:100px"> <asp:CheckBox ID="agreeCheckBox" runat="server" Width="122px" Checked="true" Text="동의합니다." /> </td> </tr> <tr> <td style="width:60px">취미</td> <td style="width:100px"> <asp:CheckBoxList ID="hobbyCheckBoxList" runat="server" Width="202px" RepeatLayout="Flow" RepeatDirection="Horizontal" RepeatColumns="2"> <asp:ListItem Selected="True" Value="S">축구</asp:ListItem> <asp:ListItem Value="V">배구</asp:ListItem> <asp:ListItem Selected="True" Value="B">농구</asp:ListItem> </asp:CheckBoxList> </td> </tr> <tr> <td style="width:60px">성별</td> <td style="width:100px"> <asp:RadioButton ID="manRadioButton" runat="server" GroupName="Gender" Checked="true" Text="남자" /> <asp:RadioButton ID="womanRadioButton" runat="server" GroupName="Gender" Text="여자" /> </td> </tr> <tr> <td style="width:60px">결혼 여부</td> <td style="width:100px"> <asp:RadioButtonList ID="weddingRadioButtonList" runat="server" Width="110px" RepeatLayout="Flow" RepeatDirection="horizontal"> <asp:ListItem Selected="True">미혼</asp:ListItem> <asp:ListItem>기혼</asp:ListItem> </asp:RadioButtonList> </td> </tr> <tr> <td style="width:60px;height:24px">직업</td> <td style="width:100px;height:24px"> <asp:DropDownList ID="jobDropDownList" runat="server"> <asp:ListItem>회사원</asp:ListItem> <asp:ListItem Selected="True">공무원</asp:ListItem> <asp:ListItem>백수</asp:ListItem> </asp:DropDownList> </td> </tr> <tr> <td style="width:60px">특기</td> <td style="width:100px"> <asp:ListBox ID="favoriteListBox" runat="server" SelectionMode="Multiple"> </asp:ListBox> </td> </tr> </table> <br /> <asp:Button ID="okButton" runat="server" Text="확인" OnClick="okButton_Click" /><br /> <br /> <asp:Label ID="displayLabel" runat="server" /> </div> </form> </body> </html> |
▶ MainPage.aspx.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
using System; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : Page { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected //////////////////////////////////////////////////////////////////////////////// Event #region 페이지 로드시 처리하기 - Page_Load(sender, e) /// <summary> /// 페이지 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> protected void Page_Load(object sender, EventArgs e) { if(!Page.IsPostBack) { SetFavoriteListBoxData(); } } #endregion #region 확인 버튼 클릭시 처리하기 - okButton_Click(sender, e) /// <summary> /// 확인 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> protected void okButton_Click(object sender, EventArgs e) { StringBuilder stringBuilder = new StringBuilder(); if(this.agreeCheckBox.Checked) { stringBuilder.Append("[1] " + this.agreeCheckBox.Text + "<br />"); } if(this.hobbyCheckBoxList.Items[0].Selected) { stringBuilder.Append("[2] " + this.hobbyCheckBoxList.Items[0].Value + "<br />"); } if(this.hobbyCheckBoxList.Items[1].Selected) { stringBuilder.AppendFormat("[2] {0}<br/>", this.hobbyCheckBoxList.Items[1].Value); } if(this.hobbyCheckBoxList.Items[2].Selected) { stringBuilder.AppendFormat("[2] {0}<br/>", this.hobbyCheckBoxList.Items[2].Value); } if(this.manRadioButton.Checked) { stringBuilder.AppendFormat("[3] {0} 선택<br />", this.manRadioButton.Text); } else { stringBuilder.AppendFormat("[3] {0} 선택<br />", this.womanRadioButton.Text); } stringBuilder.AppendFormat("[4] {0}<br />", this.weddingRadioButtonList.SelectedItem.Text); stringBuilder.AppendFormat("[5] {0}<br />", this.jobDropDownList.Items[this.jobDropDownList.SelectedIndex].Value); for(int i = 0; i < this.favoriteListBox.Items.Count; i++) { if(this.favoriteListBox.Items[i].Selected) { stringBuilder.AppendFormat("[6] {0}<br />", this.favoriteListBox.Items[i].Value); } } foreach(ListItem listItem in this.favoriteListBox.Items) { if (listItem.Selected) { stringBuilder.AppendFormat("[6] {0}<br />", listItem.Text); } } this.displayLabel.Text = stringBuilder.ToString(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 특기 리스트 박스 데이터 설정하기 - SetFavoriteListBoxData() /// <summary> /// 특기 리스트 박스 데이터 설정하기 /// </summary> private void SetFavoriteListBoxData() { this.favoriteListBox.Items.Add("C#"); this.favoriteListBox.Items.Add("ASP.NET"); ListItem listItem = new ListItem(); listItem.Text = "자바스크립트"; listItem.Value = "JavaScript"; this.favoriteListBox.Items.Add(listItem); listItem = new ListItem("닷넷", ".NET"); this.favoriteListBox.Items.Add(listItem); } #endregion } } |