Blog Stats
  • Posts - 18
  • Articles - 0
  • Comments - 583
  • Trackbacks - 69

 

Un-hiding web control declarations in ASP.NET 2.0 code-behind files

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.


Feedback

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files

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
9/3/2007 4:28 AM | Bharati

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files

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. 9/6/2007 7:18 PM | Billy McCafferty

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files

Gravatar Is there anyway to see the hidden partial contents? I'd like to use the partial class as a starting point.

Thanks! 1/25/2008 8:08 AM | Joel

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files

Gravatar Good question Joel...I'm not sure what the answer is though. 1/25/2008 8:17 AM | Billy McCafferty

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files

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. 2/21/2008 11:08 PM | jake

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files

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 2/29/2008 8:55 PM | KM Websol

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files

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. 9/23/2010 2:25 AM | Halloween Contacts

# re: Un-hiding web control declarations in ASP.NET 2.0 code-behind files

Gravatar again a good tip. Thank you Billy :) 11/9/2010 5:46 AM | paris sportif

Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

 

 

Copyright © Billy McCafferty