본문 바로가기

IT/ASP

[ASP] 기초 문법


문자열 출력

<html>
<body>
<%
response.write("My first ASP script!")
%>
</body>
</html>



변수와 문자열 출력

변수와 문자를 연결할때 &을 사용합니다.

<html>
<body>
<%
dim name
name="Donald Duck"
response.write("My name is: " & name)
%>
</body>
</html>



배열

<html>
<body>
<%
Dim famname(5),i
famname(0) = "Jan Egil"
famname(1) = "Tove"
famname(2) = "Hege"
famname(3) = "Stale"
famname(4) = "Kai Jim"
famname(5) = "Borge"

For i = 0 to 5
         response.write(famname(i) & "<br />")
Next
%>
</body>
</html>



if문

<html>
<body>
<%
dim h
h=hour(now())

response.write("<p>" & now())
response.write("</p>")
If h<12 then
   response.write("Good Morning!")
else
   response.write("Good day!")
end if
%>
</body>
</html>



for문

<html>
<body>
<%
dim i
for i=1 to 6
   response.write("<h" & i & ">Heading " & i & "</h" & i & ">")
next
%>
</body>
</html>



리턴값이 없는 함수

<html>
<head>
<%
sub vbproc(num1,num2)
response.write(num1*num2)
end sub
%>
</head>
<body>

<p>Result: <%vbproc 3,4%></p>

</body>
</html>



리턴값이 있는 함수

함수명과 동일한 변수명에 리턴하고자 하는 값을 넣어 함수를 종료하면 됩니다.

<%
Function Add(num1, num2)
sum = num1 + num2
Add = sum
end function
%>



페이지이동

설마 요즘세상에도 서버측에서 사용자 액션에 따른 DB작업 수행후 자바스크립트 location.href로 페이지
이동시키는분 안계시죠?


<%
Response.Redirect "http://www.w3schools.com"
%>



이스케이프

ASP에서 문자열을 출력할때 쌍따옴표를 쓰는데 출력할 문자열에 쌍따옴표가 포함되어 있을때는 쌍따옴표를
그 앞에 한번 더 써주시면 쌍따옴표가 한번만 정상적으로 출력됩니다.


Response.Write "Raju told ""ASP is a better scripting language"""

위와 같은 문장을 출력하면

Raju told "ASP is a better scripting language"

이와 같이 출력됩니다.


줄바꿈 적용

php의 함수로 예를들면 nl2br() 함수와 같은 역할을 하는 방법입니다.

replace(rs(), char(13)&chr(10),"<br />");


파일 인클루드 하기

가상디렉토리경로로 인클루드 하기

<!-- #include virtual ="/html/header.inc" -->

상대경로로 인클루드하기

<!-- #include file ="headers\header.inc" -->

이렇게는 안됩니다.

불러오고자 하는 파일명을 변수로 설정한 경우 실행되지 않습니다.

<%
fname="header.inc"
%>
<!--#include file="<%fname%>"-->

출처 : http://trend21c.tistory.com