Theory
Two questions every page answers
Every FestConnect page has to answer two different questions: what does it look like (the layout: headings, text boxes, a Register button) and what does it do (the logic: save the registration, greet the user). ASP.NET lets you keep these two concerns in the right places.
The layout lives in an .aspx file, written in markup. The logic is C# code, and you have a choice of where to put that code: mixed into the same .aspx file (inline), or in a separate partner file (code-behind). This lesson compares the two and shows how the CLR turns either one into a running page.
Theory
Inline coding
Inline coding places the C# directly inside the .aspx file, typically in a <script runat="server"> block alongside the markup.
Everything for the page sits in one file. That is convenient for a tiny demo, but it mixes layout and logic together, which quickly becomes hard to read and maintain as a page grows. Think of it like writing your styling, your HTML, and your program all in one long file: fine for five lines, painful for five hundred.
Practical
Inline: C# inside the .aspx file
<%@ Page Language="C#" %>
<script runat="server">
// Logic lives in the same file as the markup
void Greet(object sender, EventArgs e) {
lblMessage.Text = "Welcome to FestConnect";
}
</script>
<html>
<body>
<form runat="server">
<asp:Label id="lblMessage" runat="server" />
<asp:Button Text="Greet" OnClick="Greet" runat="server" />
</form>
</body>
</html>This example runs in Gri-Learn on the web, where you can edit it and see the output.
Theory
Code-behind
Code-behind puts the C# in a separate file that partners the page. For a page named Default.aspx, the code lives in Default.aspx.cs. The .aspx keeps only markup; the .aspx.cs keeps only logic. A directive at the top of the .aspx links them.
This is the standard, recommended approach. It cleanly separates layout from logic, so designers can work on the markup and programmers on the code without colliding, and each file stays focused and readable. Every real FestConnect page uses code-behind.
Practical
Code-behind: logic in Default.aspx.cs
using System;
// The .aspx page links here via its CodeFile and Inherits directive
public partial class Default : System.Web.UI.Page
{
protected void Greet(object sender, EventArgs e)
{
lblMessage.Text = "Welcome to FestConnect";
}
}
// The markup (labels, buttons) stays in Default.aspx, kept separate and clean.Formula
The CLR still compiles it
Either way, your page code is compiled, not interpreted line by line. This is the same Common Language Runtime (CLR) from BCA404: your C# is compiled to MSIL (an intermediate language), then just-in-time compiled to native machine code and run.
ASP.NET compiles a page the first time it is requested and reuses that compiled version afterwards, so the first hit is slightly slower and the rest are fast. The takeaway: ASP.NET is compiled .NET, with all the type-checking and speed that brings.
Quiz
What is the main difference between code-behind and inline coding in ASP.NET?
- Code-behind runs in the browser, while inline coding runs on the server
- Code-behind puts the C# in a separate partner file from the .aspx markup, while inline coding mixes the C# into the .aspx itself
- Inline coding is compiled by the CLR, while code-behind is not compiled at all
- There is no real difference; the two names mean the same thing
Show the answer
Code-behind puts the C# in a separate partner file from the .aspx markup, while inline coding mixes the C# into the .aspx itself
The difference is WHERE the code lives. Code-behind places the C# in a separate partner file (Default.aspx.cs) linked to the markup, cleanly separating layout from logic. Inline coding puts the C# inside the .aspx file itself, mixing the two. Option A is wrong: both run on the server (all ASP.NET page code does), not the browser. Option C is wrong: BOTH are compiled by the CLR; where the code sits does not change that. Option D is wrong: they genuinely differ, and code-behind is the recommended approach because it keeps files focused and maintainable.
At a glance
| Aspect | Inline coding | Code-behind |
|---|---|---|
| Where the C# lives | Inside the .aspx file | In a separate .aspx.cs file |
| Layout and logic | Mixed together | Cleanly separated |
| Best for | Tiny demos and quick tests | Real applications like FestConnect |
| Maintainability | Gets messy as the page grows | Stays readable and organised |
| Compiled by CLR | Yes | Yes |
Think first
Why is separating layout from logic worth the extra file?
Code-behind means two files instead of one. Why is that separation considered better practice? Then tap.
Show the answer
Because SEPARATION OF CONCERNS makes software easier to build, read, and change. When the visual layout is in the .aspx and the program logic is in the .aspx.cs, each file has one clear job. A designer can restyle the registration form without touching, or risking, the code that saves the data; a programmer can fix the save logic without wading through markup. Bugs are easier to find because you know which file to look in. As FestConnect grows to dozens of pages, this discipline keeps it maintainable, whereas inline code turns every large page into a tangled mix that is hard to edit safely. It is the same principle you have met before: keep distinct responsibilities in distinct places. One file, one job.
Summary
Key takeaways
- An ASP.NET page splits into markup (.aspx, the layout) and code (C#, the logic).
- Inline coding puts the C# inside the .aspx file, using a script runat=server block; fine for tiny demos only.
- Code-behind puts the C# in a separate partner file (Default.aspx.cs) linked by a page directive; this is the recommended approach.
- Code-behind separates layout from logic, keeping each file focused and maintainable.
- Either way, the CLR compiles the page: C# to MSIL, then just-in-time to native code (the same CLR as BCA404).
- ASP.NET compiles a page on its first request and reuses the compiled version after.
- Memory hook: code-behind = logic in its own file, next to the markup, not inside it.