- ASP.NET 3.5 Application Architecture and Design
- Vivek Thakur
- 366字
- 2021-05-28 17:47:11
Sample Project using Code-Behind
Here is an example of the same guestbook application being coded using code-behind classes:
using System; using System.Data; using System.Data.OleDb; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace Chapter2.CodeBehindStyle { public partial class Default : System.Web.UI.Page { protected void btnComments_Click(object sender, EventArgs e) { LoadComments(); } /// <summary> /// Load all comments from the Access DB /// </summary> private void LoadComments() { string AppPath = System.AppDomain.CurrentDomain. BaseDirectory.ToString(); string sCon = @"Provider=Microsoft.JET.OLEDB.4.0; Data Source=" + AppPath + "/App_Data/Guestbook.mdb"; using (OleDbConnection cn = new OleDbConnection(sCon)) { string sQuery = @"SELECT * FROM Guestbook order by EntryDate desc"; OleDbCommand cmd = new OleDbCommand(sQuery, cn); OleDbDataAdapter da = new OleDbDataAdapter(cmd); DataSet ds = new DataSet(); cn.Open(); da.Fill(ds); rptComments.DataSource = ds; rptComments.DataBind(); } } }//end class }//end namespace
In the code above we are simply loading all guestbook entries in the LoadComments()
method and binding it to a repeater (rptComments) in the code-behind partial class file.
Here is the ASPX form:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Chapter2.CodeBehindStyle.Default" %> <html> <head> <title>Chapter 2: Code-behind sample in ASP.NET</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btnComments" runat="server" Text="View All Comments" OnClick="btnComments_Click" /> <h1>Guestbook Entries</h1> <asp:Repeater id="rptComments" runat="server"> <ItemTemplate> Name: <%# Eval("FullName")%> <br> Email: <%# Eval("EmailID")%> <br> Website: <%# Eval("Website")%> <br> Dated: <%# Eval("EntryDate")%> <br> Comments: <%# Eval("Comments")%> </ItemTemplate> </asp:Repeater> </div> </form> </body> </html>
As you can see, we don't have any C# or VB.NET coding in the ASPX pages; all of the managed code is in the code-behind class. Also note that the declaration of any server side control is put in a separate designer.cs
file, which is auto-generated after parsing the ASPX markup file. So we have a clean logical separation of the declarative ASPX controls placement (HTML part) and the actual managed code in the code-behind partial classes. Here is a diagrammatic representation of this 1-tier 1-layer style having 2 sub-layers in the main UI layer:
UI Layer

From the given diagram, we can see that we still have one single physical DLL, but the UI code itself is logically separated into two layers—one in the markup code and the other in the code-behind file. Note that, as explained in the beginning of this chapter, this architecture is 1-tier from the application's viewpoint, but overall it is still 3-tier if we consider the client browser as the presentation tier, and the database as the data tier. We are only focusing on the application tier, which houses the UI layer in the above examples.
- AutoCAD 2022快速入門、進階與精通
- 邊做邊學:Photoshop圖像制作案例教程(Photoshop CC 2019·微課版)
- Moldflow 2010完全自學與速查手冊(模流分析·成本控制)
- Getting Started With Oracle SOA Suite 11g R1 – A Hands/On Tutorial
- ADOBE FLASH PROFESSIONAL CS6 標準培訓教材
- PyTorch深度學習簡明實戰(zhàn)
- Illustrator 2024從入門到精通
- MATLAB 2022a從入門到精通
- OpenCV項目開發(fā)實戰(zhàn)(原書第2版)
- Indesign平面排版技術(shù)應用
- 巧學巧用Flash CS6制作動畫
- 虛擬現(xiàn)實:沉浸于VR夢境
- MATLAB在日常計算中的應用
- Photoshop-CorelDRAW 基礎(chǔ)培訓教程
- Photoshop+CorelDRAW平面設(shè)計實例教程(第3版)