Understanding Code Behind

You might have noticed the Default.aspx.cs file in the PersonalPortal WebSites directory. You can also find the file underneath the Default.aspx page in Solution Explorer. This is the code behind file. In Chapter 3, when you created your first ASP.NET page, you learned how to designate code to be run on the server. This code was written inline with HTML, using the <script> tag or enclosed in <% and %>.

Adding code to your pages like this is fine when the code is simple and short. However, as code becomes longer and more complex, it can become difficult to maintain. Code behind is a way to separate code from markup to ease developing and maintaining complex code.

If you look at the code of Default.aspx in Example 4-1, it ...

Get Microsoft® Visual Web Developer™ 2008 Express Edition Step by Step now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.