- ASP.NET 3.5 Application Architecture and Design
- Vivek Thakur
- 561字
- 2025-03-31 06:56:00
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.