Geeks With Blogs
Billy McCafferty whatever (but really just .NET)

ASP.NET has an “interesting” way of encapsulating web control declarations within the code-behind of an ASPX page.  Instead of forcing you (or “allowing you,” based on your perspective) to explicitly declare web controls via protected members in your code behind page, ASP.NET 2.0 automatically associates a hidden partial class with your code-behind that contains the explicit declarations.

I appreciate the intention, but I find that the implementation makes the code-behind less clear by hiding the control declarations from plain view.  Assuming you're not writing monolithic code-behind classes, this coding convenience rarely saves more than a few lines of code.  Additionally, with this model, you can no longer add attributes to your control declarations.  (For example, some web frameworks allow you to extend ASP.NET's capabilities by placing attibutes along with your control declarations to support features such as bi-directional data binding.)

If you need to control of the declaration of your web controls, or simply miss seeing your declarations in the code-behind, there's a simple fix:  within the page declaration of an ASPX page is the keyword CodeFile - change this to src.  With this, your web control declarations will no longer be hidden and may/must be explicitly declared in the code-behind.

Posted on Monday, February 27, 2006 7:31 AM ASP.NET | Back to top


Comments on this post: Un-hiding web control declarations in ASP.NET 2.0 code-behind files

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files
Requesting Gravatar...
I cant see the web-control declarations even when I change 'CodeFile' to 'Src'.

Am I missing something else? Please let me know.

Regards
Left by Bharati on Sep 03, 2007 4:28 AM

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files
Requesting Gravatar...
After you set it to Src, you then need to manually enter the declarations into your code-behind. They won't show up automatically.
Left by Billy McCafferty on Sep 06, 2007 7:18 PM

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files
Requesting Gravatar...
Is there anyway to see the hidden partial contents? I'd like to use the partial class as a starting point.

Thanks!
Left by Joel on Jan 25, 2008 8:08 AM

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files
Requesting Gravatar...
Good question Joel...I'm not sure what the answer is though.
Left by Billy McCafferty on Jan 25, 2008 8:17 AM

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files
Requesting Gravatar...
my scenario is i'm adding an existing file into a solution. when i do a build it gives this error; that the control definitions are hidden. i tried changing the codefile to src in the page definition; it was sorta bad since when i went back to the .cs file for the page the System.Web namespace and a lot of others were unavailable. I changed the Src back to CodeFile and then I was able to see all framework namespaces and then i declared my controls. The build then succeeded.
Left by jake on Feb 21, 2008 11:08 PM

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files
Requesting Gravatar...
i am new in asp.net and i am using c# for back end and i found one VB code i dont no how to declare below code in c# any help!!!!

Protected WithEvents mvwSysMessage As System.Web.UI.WebControls.MultiView
//Protected WithEvents vwError As System.Web.UI.WebControls.View
//Protected WithEvents vwSuccess As System.Web.UI.WebControls.View
//Protected WithEvents vwInfo As System.Web.UI.WebControls.View
//Protected WithEvents imgErr As System.Web.UI.WebControls.Image
//Protected WithEvents lblErr As System.Web.UI.WebControls.Label
//Protected WithEvents imgSuc As System.Web.UI.WebControls.Image
//Protected WithEvents lblSuc As System.Web.UI.WebControls.Label
//Protected WithEvents imgInf As System.Web.UI.WebControls.Image
//Protected WithEvents lblInf As System.Web.UI.WebControls.Label
Left by KM Websol on Feb 29, 2008 8:55 PM

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files
Requesting Gravatar...
I changed the Src back to CodeFile and then I was able to see all framework namespaces and then i declared my controls. The build then succeeded.
Left by Halloween Contacts on Sep 23, 2010 2:25 AM

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files
Requesting Gravatar...
again a good tip. Thank you Billy :)
Left by paris sportif on Nov 09, 2010 5:46 AM

Your comment:
 (will show your gravatar)


Copyright © Billy McCafferty | Powered by: GeeksWithBlogs.net