- ASP.NET 3.5 Application Architecture and Design
- Vivek Thakur
- 561字
- 2021-05-28 17:47:11
Sample Project using Inline Code
Let's say we have a simple online guestbook system, which is web-based and developed in ASP.NET. We have a simple web form with a button, and have all coding on the ASPX page itself (inline coding). The system will simply query a Microsoft Access database and return the results to a repeater control.
Here is the ASPX code sample:
<%@ Page Language="C#" %> <%@ Import Namespace="System" %> <%@ Import Namespace="System.Data" %> <%@ Import Namespace="System.Data.OleDb" %> <script language="C#" runat="server"> private void Page_Load(object sender, EventArgs e) { //page load coding goes here } private void btnComments_Click(object sender, EventArgs e) { LoadComments(); } private void LoadComments() { string AppPath = System.AppDomain.CurrentDomain. BaseDirectory.ToString(); string sCon = @"Provider=Microsoft.JET.OLEDB.4.0; Data Source=" + AppPath + "/App_Data/Personal.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(); } } </script>
Note that we have used <script> block for the inline C# code. Now we start the HTML code on the same page (after the </script> ends):
<html> <head runat="server"> <title>Chapter 2: Inline coding sample in ASPX</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>
In this page, the coding technique used is known as inline coding, used in old classic ASP (ASP 3.0) minus the spaghetti mix. Here, the HTML and C# code is mixed in a single file, unlike the default code-behind model used in ASP.NET. But the inline code is separately marked using the<script>
tag. This style is basically the 1-tier 1-layer style, with the application tier having the coding logic in the UI layer itself. In classic ASP, there was no easy way to debug such ASP pages, which had both HTML and ASP script mixed up, and the only way to find out if your code was working properly was to use Response.Write()
statements throughout the code
base, which made debugging a very painstaking and time-consuming process. With improvements in the IDE, VS (Visual Studio) could debug such inline code script tags, and VS 2005 upwards also supported IntelliSense in ASPX pages. But mixing code and HTML was still a bad idea for the following reasons:
- No separation of business logic, data access code, and presentation (HTML): It was therefore not possible to have a distributed architecture in the sense that the entire code base was monolithic in nature and could not be physically separated.
- Code re-use: Code cannot be re-used in other pages, whereas in code-behind files, we can call methods from a class file in many pages.
- Source Code Control (SCC) problems: A developer working on a file will need to block (check-out) the entire file. In the code-behind model, different developers can work on the UI and the code logic, as we have different files for each.
- Compilation model: Errors won't be found until the code is executed.
- Maintenance issue: Long-term maintenance will be an issue.
There were also some advantages in using this inline model, but the disadvantages above far outweighed any advantages:
- Because we are not using class files, simply updating a page will propagate changes to the server, without causing the users to log off, as no application restart will take place. So we can update without stopping the application, or causing an application restart.
- There can be a slight performance benefit compared to using assemblies, but this will be negligible, as modern day computing power is very fast.
- Creo 4.0中文版從入門到精通
- Pro/E Wildfire 5.0中文版入門、精通與實戰
- Photoshop CS5平面設計入門與提高
- 中文版Premiere Pro CC實用教程
- UG NX 12.0實例寶典
- MATLAB 2015從入門到精通
- 深入理解OpenCV:實用計算機視覺項目解析(原書第3版)
- Capture One 22 Pro高級實戰教程
- 中望3D從入門到精通
- AutoCAD 2016中文版基礎教程(全圖解視頻版)
- 從零開始:Indesign CC 2019設計基礎+商業設計實戰
- Microsoft Silverlight 4 and SharePoint 2010 Integration
- Revit建模進階標準教程(實戰微課版)
- MooTools 1.2 Beginner's Guide
- OpenGL 4.0 Shading Language Cookbook