Three Is It

Because two isn't enough and four is just too many

Everybody was a baby once, Arthur. Oh, sure, maybe not today, or even yesterday. But once. Babies, chum: tiny, dimpled, fleshy mirrors of our us-ness, that we parents hurl into the future, like leathery footballs of hope. And you've got to get a good spiral on that baby, or evil will make an interception.
The Tick
Home Blogs Genealogy Brad's Bookshelf Subscriptions Contact Sign in
 

About the author

Brad Butts is a .NET developer and architect. He is married with children and enjoys reading, working out, and genealogy is his five minutes of spare time.
E-mail me Send mail
National Debt Clock

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Who needs the CodeBehind attribute?

Maybe this is obvious to everyone but me because I've found no discussions on the topic...or maybe my googling skills just stink...or maybe both...but after looking at the markup in some of SharePoint's ASPX pages and seeing neither a CodeBehind nor a CodeFile attribute in the Page Directive, it occurred to me that at least precompiled ASPX pages don't need either of these attributes.  The required attribute is Inherits.  Presumably, the .NET runtime uses the fully qualified assembly name in the Inherits attribute to discover the code behind for the requested page.  

Here's what I mean...

When I start a new Web Application Project in Visual Studio 2008 (or add a new page to such a project) and look at the markup in the ASPX page, I see a page directive declaration like this:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Examples.CodeBehindAttrTest.Default" %>

I can jump to the code-behind file (Default.aspx.cs) and add this code to the Page_Load event:

protected void Page_Load(object sender, EventArgs e)
{
Response.Write(String.Format("The time is: {0}", System.DateTime.Now.ToString()));
}


I can compile and run my project, browse to the default.aspx page, and see the timestamp print out with no problem.  

So, what happens when I remove the CodeBehind attribute?  Well, the very same behavior happens.  This makes sense, too, because I don't deploy CS files to my production Web servers yet I make great use of code-behind files.  Of course, Microsoft basically says as much:

"This attribute [CodeBehind] is not used at run time." (from the Page Directive reference)

For your Web Site projects, I assume you could equally remove the CodeFile attribute, but that appears much stickier, what with having to precompile the site, setting the Inherits attribute correctly, and all.

The drawback of removing the CodeBehind attribute from your pages seems to be one of development-time convenience.  Without knowing what code file represents a given ASPX page, should I, say, try to add a click event handler to my server side button by double-clicking the button in the Design tab, Visual Studio will add server-side code inline to my ASPX page instead of the default behavior of taking me to my code-behind page.  If I cut and paste the inline code, drop it into my code behind page, recompile, and run the app, the event will fire as coded in my compiled DLL.  Visual Studio is even smart enough to declare my button control in the designer partial class.

Currently rated 3.0 by 2 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:
Categories: Technology Blog
Posted by Brad on Friday, September 12, 2008 5:30 PM
Permalink | Comments (10) | Post RSSRSS comment feed

Related posts

Comments

Kentucky movers gb

Sunday, June 07, 2009 1:42 PM

Kentucky movers

nice post
made my day
love u all

israel constitution uy

Sunday, June 07, 2009 1:43 PM

israel constitution

we nee more ppl like u sir
wonderful tjinking

israel elections ua

Sunday, June 07, 2009 1:43 PM

israel elections

we need more ppl like it

Dental Implants Cheshire gb

Friday, July 31, 2009 7:39 AM

Dental Implants Cheshire

Waiting for your next article my friend

Cosmetic Surgery Wrexham gb

Monday, August 10, 2009 1:19 AM

Cosmetic Surgery Wrexham

The style and the nature of your content is really great! Thanks.

Transfer of Equity gb

Monday, August 10, 2009 7:19 AM

Transfer of Equity

It is indeed a great resource to obtain information on this subject. Keep posting. Thanks.

Dentist Northern Ireland gb

Thursday, August 13, 2009 3:17 AM

Dentist Northern Ireland

Very good and useful source for the best and latest information and related stuff.

Cosmetic Surgery Liverpool gb

Wednesday, September 02, 2009 5:41 AM

Cosmetic Surgery Liverpool

now these are becoming one of the better blog platforms out there

Dentists Birmingham gb

Friday, September 04, 2009 3:52 AM

Dentists Birmingham

Applications information detailed with useful guidelines and helping concepts.

Cosmetic Surgeons London gb

Monday, September 07, 2009 5:56 AM

Cosmetic Surgeons London

Helping tips and ideas shared nicely with useful guidelines and concepts.

Comments are closed