2-2. 표준컨트롤, 서버컨트롤
FrmAdRotator.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmAdRotator.aspx.cs" Inherits="FrmAdRotator" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>광고컨트롤</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:AdRotator ID="AdRotator1" runat="server" Width="500px" Height="180px" AdvertisementFile="~/FrmAdRotator.xml" />
</div>
</form>
</body>
</html>
FrmAdRotator.xml
<?xml version="1.0" encoding="utf-8" ?>
<Advertisements>
<Ad>
<ImageUrl>~/images/banner1.gif</ImageUrl>
<NavigateUrl>http://www.VisualAcademy.com/</NavigateUrl>
<AlternateText>비주얼아카데미</AlternateText>
<Keyword>비주얼아카데미</Keyword>
<Impressions>25</Impressions>
</Ad>
<Ad>
<ImageUrl>~/images/banner2.gif</ImageUrl>
<NavigateUrl>http://www.dotnetkorea.com/</NavigateUrl>
<AlternateText>닷넷코리아</AlternateText>
<Keyword>닷넷코리아</Keyword>
<Impressions>25</Impressions>
</Ad>
<Ad>
<ImageUrl>~/images/banner3.gif</ImageUrl>
<NavigateUrl>http://www.dotnetkorea.com/</NavigateUrl>
<AlternateText>닷넷코리아</AlternateText>
<Keyword>닷넷코리아</Keyword>
<Impressions>25</Impressions>
</Ad>
<Ad>
<ImageUrl>~/images/banner4.gif</ImageUrl>
<NavigateUrl>http://www.dotnetkorea.com/</NavigateUrl>
<AlternateText>닷넷코리아</AlternateText>
<Keyword>닷넷코리아</Keyword>
<Impressions>25</Impressions>
</Ad>
</Advertisements>
FrmCalendar.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmCalendar.aspx.cs" Inherits="FrmCalendar" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>제목 없음</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Calendar ID="Calendar1" runat="server"
onselectionchanged="Calendar1_SelectionChanged"></asp:Calendar>
<br />
<asp:Label ID="lblDate" runat="server" Text=""></asp:Label>
<hr />
<asp:Calendar ID="Calendar2" runat="server" SelectionMode="DayWeekMonth"
onselectionchanged="Calendar2_SelectionChanged"></asp:Calendar>
<br />
<asp:Label ID="lblDates" runat="server" Text=""></asp:Label>
<br />
<asp:Calendar ID="Calendar3" runat="server" BackColor="White"
BorderColor="White" BorderWidth="1px" Font-Names="Verdana" Font-Size="9pt"
ForeColor="Black" Height="190px" NextPrevFormat="FullMonth" Width="258px">
<SelectedDayStyle BackColor="#333399" ForeColor="White" />
<TodayDayStyle BackColor="#CCCCCC" />
<OtherMonthDayStyle ForeColor="#999999" />
<NextPrevStyle Font-Bold="True" Font-Size="8pt" ForeColor="#333333"
VerticalAlign="Bottom" />
<DayHeaderStyle Font-Bold="True" Font-Size="8pt" />
<TitleStyle BackColor="White" BorderColor="Black" BorderWidth="4px"
Font-Bold="True" Font-Size="12pt" ForeColor="#333399" />
</asp:Calendar>
</div>
</form>
</body>
</html>
FrmCalendar.aspx.cs
using System;
public partial class FrmCalendar : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
// 현재 선택된 날짜/시간값을 출력
this.lblDate.Text = Calendar1.SelectedDate.ToLongDateString();
}
protected void Calendar2_SelectionChanged(object sender, EventArgs e)
{
// 현재 선택된 범위의 날짜값을 출력
string s = "";
foreach (DateTime item in Calendar2.SelectedDates)
{
s += item.ToShortDateString() + "<br />";
}
lblDates.Text = s;
}
}
FrmTextBox.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmTextBox.aspx.cs" Inherits="FrmTextBox" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>제목 없음</title>
</head>
<body>
<form id="form1" runat="server">
<div>
SingleLine:<br />
이름 :
<asp:TextBox ID="txtSingleLine" runat="server"></asp:TextBox>
<br />
<br />
MultiLine<br />
소개 :<asp:TextBox ID="txtMultiLine" runat="server" TextMode="MultiLine"
Rows="5" Columns="40"></asp:TextBox>
<br />
<br />
Password<br />
암호 :<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnOK" runat="server" Text="확인" onclick="btnOK_Click" /><br />
<asp:Label ID="lblDisplay" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
FrmTextBox.aspx.cs
using System;
public partial class FrmTextBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnOK_Click(object sender, EventArgs e)
{
// 이름
string name = txtSingleLine.Text;
// 소개
string intro = txtMultiLine.Text;
// 암호
string password = txtPassword.Text;
// 레이블에 출력
this.lblDisplay.Text = String.Format("{0}{1}{2}{1}{3}{1}", name, "<br />"
, intro.Replace("\r\n", "<br />"), password);
}
}
[Transform : 기본]
FrmLiteral.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmLiteral.aspx.cs" Inherits="FrmLiteral" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>리터럴 : 문자열/태그 출력</title>
</head>
<body>
<form id="form1" runat="server">
<div>
HTML :
<span>순수 태그로 출력</span>
<br />
레이블 :
<asp:Label ID="Label1" runat="server" Text="레이블로 출력"></asp:Label>
<br />
<asp:Label ID="Label2" runat="server" Text="<hr />"></asp:Label>
<br />
리터럴 : 자리 역할, 원하는 위치에 무엇인가 출력해 주고 싶을 때
<asp:Literal ID="Literal1" runat="server" Text="리터럴 컨트롤"></asp:Literal>
<br />
<asp:Literal ID="Literal3" runat="server" Text="<hr />"></asp:Literal>
<br />
<asp:Literal ID="Literal2" runat="server" Text="<hr />" Mode="Encode"></asp:Literal>
<!--Mode="Encode"로 하면 <hr />을 실행하지 않고 그대로 보여줌
레이블 컨트롤은 실행 했을 때 모든 태그를 span태그로 묶음
리터럴은 아무런 태그도 만들어지지 않고 원하는 자리에 출력 가능.
태그로 묶이지 않기 때문에 간결
일반적으로 레이블 컨트롤 보다 리터럴 컨트롤을 권장함
게시판의 답변달 때 리터럴 컨트롤 사용-->
</div>
</form>
</body>
</html>
FrmLinkButton.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmLinkButton.aspx.cs" Inherits="FrmLinkButton" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>링크 버튼 : 하이퍼링크</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="http://www.dotnetkorea.com/" target="_blank">닷넷코리아</a>
<hr />
<asp:LinkButton ID="lnkVisualAcademy" runat="server"
onclick="lnkVisualAcademy_Click">비주얼아카데미</asp:LinkButton>
<hr />
<asp:HyperLink ID="lnkJavaCampus" runat="server" NavigateUrl="http://www.JavaCampus.com">자바캠퍼스</asp:HyperLink>
<hr />
<asp:ImageButton ID="imgDotNetNote" runat="server" ImageUrl="http://www.dotnetkorea.com/images/btn_home.gif"
Width="32px" Height="32px" AlternateText="홈페이지로 이동"
onclick="imgDotNetNote_Click" />
<br />
<!--서버 컨트롤 :기본 값으로 설정해 둔 값을 실행 시에 동적으로 바꿔 줄 수 있음-->
</div>
</form>
</body>
</html>
FrmLinkButton.aspx.cs
using System;
using System.Web.UI;
public partial class FrmLinkButton : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// HTML과 달리 서버컨트롤(runat=server)들은 런타임 시에 동적으로 변경 가능
if (DateTime.Now.Second % 2 == 0)
{
this.lnkJavaCampus.Text = "짜빠 캠퍼스";
}
else
{
this.lnkJavaCampus.Text = "자바 캠퍼스";
}
}
protected void lnkVisualAcademy_Click(object sender, EventArgs e)
{
// 링크 버튼은 이벤트 처리기 사용
Response.Redirect("http://www.VisualAcademy.com");
}
protected void imgDotNetNote_Click(object sender, ImageClickEventArgs e)
{
// EventArgs앞에 무언가 붙으면 e. 찍으면 무언가 얻을 수 있음
// 이미지를 기준으로 좌표값을 나눠줌
if (e.X < 16)
{
Response.Redirect("http://www.dotnetkorea.com/");
}
else
{
Response.Redirect("http://www.visualacademy.com/");
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmRequiredFieldValidator.aspx.cs" Inherits="FrmRequiredFieldValidator" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
입력 확인 유효성 검사 컨트롤<br />
<br />
<br />
아이디 :<asp:TextBox ID="txtUserID" runat="server"></asp:TextBox>
[<asp:RequiredFieldValidator ID="valUserID" runat="server" ControlToValidate="txtUserID" ValidationGroup="Login" Display="Dynamic"
ErrorMessage="아이디를 입력하시오."></asp:RequiredFieldValidator>]
<br />
암호 :<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
[<asp:RequiredFieldValidator ID="valPassword" runat="server" ControlToValidate="txtPassword" ValidationGroup="Login" Display="Static"
ErrorMessage="암호를 입력하시오."></asp:RequiredFieldValidator>]
<br />
<asp:Button ID="btnLogin" runat="server" Text="로그인" ValidationGroup="Login" />
</div>
</form>
</body>
</html>
- CompareValidator
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmCompareValidator.aspx.cs" Inherits="FrmCompareValidator" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
비교<br />
<br />
암호 :
<asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ControlToValidate="txtPassword"
ControlToCompare="txtPasswordConfirm"
ErrorMessage="암호를 확인하세요."></asp:CompareValidator>
<br />
암호확인 :<asp:TextBox ID="txtPasswordConfirm" runat="server"></asp:TextBox>
<br />
<br />
<asp:LinkButton ID="btnLogin" runat="server">로그인</asp:LinkButton>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmRangeValidator.aspx.cs" Inherits="FrmRangeValidator" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
범위<br />
<br />
나이 :
<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
(1~150)
<asp:RangeValidator ID="valAge" runat="server"
ControlToValidate="txtAge"
MinimumValue="1"
MaximumValue="150"
Type="Integer"
ErrorMessage="나이는 1~150까지 입력하시오."></asp:RangeValidator>
<br />
<br />
<asp:Button ID="btnLogin" runat="server" Text="확인" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmRegularExpressionValidator.aspx.cs" Inherits="FrmRegularExpressionValidator" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
정규식(정규표현식) 검사?<br />
이메일 :
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RegularExpressionValidator ID="valEmail" runat="server"
ControlToValidate="txtEmail"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
ErrorMessage="이메일을 정확히"></asp:RegularExpressionValidator>
<br />
<br />
<asp:Button ID="btnOK" runat="server" Text="확인" />
</div>
</form>
</body>
</html>
FrmCustomerValidator.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmCustomValidator.aspx.cs" Inherits="FrmCustomValidator" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script> // 클라이언트 레벨이라 서버에 부하를 줄여줌
function ClientValidate(sender, args) {
if (args.Value.length < 3 || args.Value.length > 12) {
args.IsValid = false;
}
else {
args.IsValid = true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
아이디 :
<asp:TextBox ID="txtUserID" runat="server"></asp:TextBox><br />
<asp:Button ID="btnOK" runat="server" Text="확인" />
<asp:CustomValidator ID="CustomerValidator1" runat="server"
ControlToValidate="txtUserID"
ClientValidationFunction="ClientValidate"
ErrorMessage="아이디는 3자 이상 12자 이하"
onservervalidate="valCustomer_ServerValidate"></asp:CustomValidator>
</div>
</form>
</body>
</html>
FrmCustomerValidator.aspx.cs
using System;
using System.Web.UI.WebControls;
public partial class FrmCustomValidator : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void valCustomer_ServerValidate(object source, ServerValidateEventArgs args)
{
// 서버 컨트롤로 할 수 있는 건 최대한 자바스크립트로 처리
// 서버의 부하를 줄여주기 위해서
string userID = args.Value; // 지정된 텍스트박스의 값을 받아서
if (userID.Length < 3 || userID.Length > 12)
{
args.IsValid = false; // 에러
}
else
{
args.IsValid = true; // 유효성 통과
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FrmValidationSummary.aspx.cs" Inherits="FrmValidationSummary" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
아이디 :
<asp:TextBox ID="txtUserID" runat="server"></asp:TextBox>
<br />
이름 :
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnOK" runat="server" Text="확인" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txtUserID" Display="None"
ErrorMessage="아이디 입력"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="valName" runat="server"
ControlToValidate="txtName" Display="None"
ErrorMessage="이름 입력"></asp:RequiredFieldValidator>
<asp:ValidationSummary ID="valSummary" runat="server"
ShowMessageBox="true" ShowSummary="false"/>
</div>
</form>
</body>
</html>
Default.aspx
Navigator.ascx : 상위 메뉴
Category.ascx : 하부 메뉴
Catalog.ascx : 상품 진열
Copylight.ascx : 회사 소개
About.aspx
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register src="Navigator.ascx" tagname="Navigator" tagprefix="uc1" %>
<%@ Register src="Category.ascx" tagname="Category" tagprefix="uc2" %>
<%@ Register src="Catalog.ascx" tagname="Catalog" tagprefix="uc3" %>
<%@ Register src="Copylight.ascx" tagname="Copylight" tagprefix="uc4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1" width="100%">
<tr>
<td colspan="2">
<uc1:Navigator ID="Navigator1" runat="server" />
</td>
</tr>
<tr>
<td style="width:185px;height:300px;">
<uc2:Category ID="Category1" runat="server" />
</td>
<td>
<uc3:Catalog ID="Catalog1" runat="server" />
</td>
</tr>
<tr>
<td colspan="2">
<uc4:Copylight ID="Copylight1" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Navigator.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Navigator.ascx.cs" Inherits="Navigator" %>
<div>
<a href="Default.aspx">Home</a> | <a href="About.aspx">About</a>
</div>
Category.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Category.ascx.cs" Inherits="Category" %>
<div>
<h3>카테고리</h3>
<ul>
<li>C#</li>
<li>ASP.NET</li>
<li>Silverlight</li>
</ul>
<asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
</div>
Catalog.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Catalog.ascx.cs" Inherits="Catalog" %>
<div>
<h1>여기는 메인 페이지</h1>
</div>
Copylight.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="Copylight.ascx.cs" Inherits="Copylight" %>
<div style="text-align:center;">
Copyright© All right reserved.
</div>
About.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="About.aspx.cs" Inherits="About" %>
<%@ Register src="Navigator.ascx" tagname="Navigator" tagprefix="uc1" %>
<%@ Register src="Category.ascx" tagname="Category" tagprefix="uc2" %>
<%@ Register src="Catalog.ascx" tagname="Catalog" tagprefix="uc3" %>
<%@ Register src="Copylight.ascx" tagname="Copylight" tagprefix="uc4" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table border="1" width="100%">
<tr>
<td colspan="2">
<uc1:Navigator ID="Navigator1" runat="server" />
</td>
</tr>
<tr>
<td style="width:185px;height:300px;">
<uc2:Category ID="Category1" runat="server" />
</td>
<td>
<h3>여기는 소개 페이지 입니다.</h3>
</td>
</tr>
<tr>
<td colspan="2">
<uc4:Copylight ID="Copylight1" runat="server" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
'ASP.NET' 카테고리의 다른 글
3-2 마스터 페이지 (0) | 2018.12.05 |
---|---|
3-1. 테마 (0) | 2018.12.05 |
2-1. 웹폼 (0) | 2018.12.05 |
1. 닷넷 프레임워크 (0) | 2018.12.05 |
ASP.NET Index (0) | 2018.12.05 |