Code-behind model
Microsoft recommends dealing with dynamic program code by using the code-behind model, which places this code in a separate file or in a specially designated script tag. Code-behind files typically have names like MyPage.aspx.cs or MyPage.aspx.vb while the page file is MyPage.aspx (same filename as the page file (ASPX), but with the final extension denoting the page language). This practice is automatic in Microsoft Visual Studio and other IDEs. When using this style of programming, the developer writes code to respond to different events, like the page being loaded, or a control being clicked, rather than a procedural walk through of the document.ASP.NET's code-behind model marks a departure from Classic ASP in that it encourages developers to build applications with separation of presentation and content in mind. In theory, this would allow a web designer, for example, to focus on the design markup with less potential for disturbing the programming code that drives it. This is similar to the separation of the controller from the view in Model–View–Controller (MVC) frameworks.
Directives
A directive is special instructions on how ASP.NET should process the page. The most common directive is <%@ Page %> which can specify many things, such as which programming language is used for the server-side code.<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "---//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { 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>
<%@ Page Language="C#" CodeFile="SampleCodeBehind.aspx.cs" Inherits="Website.SampleCodeBehind" AutoEventWireup="true" %>
using System; namespace Website { public partial class SampleCodeBehind : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Write("Hello, world"); } } }
Imports System Namespace Website Public Partial Class SampleCodeBehind Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Response.Write("Hello, world") End Sub End Class End Namespace
No comments:
Post a Comment