ขออภัยครับผมพูดไม่เคลีย ที่ผมบอกว่า out หมายถึง ASP.NET ที่เป็น in line ครับซึ่ง security ค่อนข้างต่ำ
และการทำงานค่อนข้างช้ากว่าแบบที่ compile ผ่าน VS ครับ
ส่วน ASP.NET กับ .NET สัมพันธ์กันแบบไหน บอกได้ประมาณนี้ครับ
ASP.NET เป็น Framework ของ webapplication แบบนึง
ตัว ASP.NET เองสามารถเขียนได้ด้วยภาษาคอมพิิวเตอร์ที่เรียกว่า CLI ครับ C#,VB ก็เป็นหนึ่งในนั้นครับ
สรุปง่ายๆอีกทีว่า asp.net คือการเขียนเว็บด้วย .net language ครับ
เราเขียนได้สองแบบ คือ
1. in line ซัดด้วย code ตรงๆ เหมือน PHP
ตัวอย่างไฟล์ก็ก็
้ำhello.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "---//W3C//DTD XHTML 1.0 //EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
// Assign the datetime to label control
lbl1.Text = DateTime.Now.ToLongTimeString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Sample page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
The current time is: <asp:Label runat="server" id="lbl1" />
</div>
</form>
</body>
</html>
ซึ่งแบบนี้ที่ผมบอกว่ามันค่อนข้าง out
2. Code behind เขียนแบบนี้จะยากกว่า แต่ security จะสูงกว่า และการทำงานไวกว่า เพราะ code ได้ compile มาแล้ว
ในส่วนนี้ code จะแบ่งเป็นสามไฟล์ .aspx, .aspx.cs,.aspx.designer.cs ซึ่งแต่ละไฟล์ก็จะแบ่งไปเก็บ Layout, Source Code,Control ตามนี้
ตัวอย่างฮะ
ในไฟล์ aspx ต้องใส่ header ด้วยว่า .cs ของมันคือไฟล์ไหน
<%@ Page Language="C#" CodeFile="SampleCodeBehind.aspx.cs" Inherits="Website.SampleCodeBehind"
AutoEventWireup="true" %>
นี่คือส่วน code behind
using System;
namespace Website
{
public partial class SampleCodeBehind : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello, world");
}
}
}