ASP/PHP: Separating CODE from HTML

Status
Not open for further replies.

MrCatbert

Ars Tribunus Angusticlavius
9,107
Subscriptor
Using code-behind in ASP.NET all of your page code is in a separate file from the HTML. User controls encapsulate code, content, or both into reuseable chunks that can be inserted into your pages. For example, on our new alumni website (which I'm currently developing) all pages contain header and footer controls. Header includes a dynamic two-level menu system that displays a link to the parent in the menu hierarchy and the current location. Basic formatting for the header (logo bitmap and such) is in the main header.aspx file, while the code for displaying the dynamic content resides in the Page_Load member of the code-behind file header.ascx.cs. The left sidebar menu system is dynamic based on location, and works the same way. Basic table structure is in the mainmenu.aspx file, while the code that produces the dynamic entries within the table is in the code-behind file. To use the controls within your page you include tags in your ASPX file like

[AppNamespace:Header id="PageHeader" Link="previous.aspx" LastSection="Up One" ThisSection="Thispage" runat="server" /]
(replace the square brackets with angled ones)

With this method you can get a code monkey to handle all of your code in C#, C++, VB.NET, or what have you (for dynamic content, database access, etc.) and still have pages that your web designers (the ones who on a good day can manage some JavaScript) can copy for new pages or add content to without being thoroughly confused by a mishmash of <% ... %> tags that take up ten times as much space as the HTML to display the goods.

ASP.NET is a wonderful thing. At the moment I'm doing the pages for our message boards (borrowing heavily from UBB for the layout). With the user controls and pervasive CSS our web developers will be able to modify everything within the site except the actual data coming out of the database at runtime without having to see (much less understand) the code that makes it all work.

Which means that I can concentrate on writing code instead of updating web pages. :)
 
Status
Not open for further replies.