Thursday, March 29, 2012

Seperate Content from Code

After nearing completion of my first full .Net web application I am left with a page consisitng of around 600 lines of code, approximately split half and half between VB script and HTML page content.

Can anyone point me in the right direction of the best way to encapsulate the VB code into its own seperate file and then rerefence this from the form in the HTML page?

Hi,

you could place the code into separate aspx.vb file into its own class deriving form System.Web.UI.Page. Then putting the aspx to inherit from this code-behind class (with Inherits directive) would work.

Say you have files

page1.aspx (important ones are CodeFile and Inherits attributes)

<%@. Page Language="VB" AutoEventWireup="false" CodeFile="page1.aspx.vb" Inherits="page1" %>

...

page1.aspx.vb

Partial Class page1

Inherits System.Web.UI.Page

End Class

When you could put code into aspx.vb file into the class.

More information :http://www.asp.net/QuickStart/aspnet/doc/pages/codebehind.aspx

Of course if have code & logic which is such that it's common to more than one form, you might want to consider having a separate component (class library or in App_Code) where you could centralize logic that is needed in multiple places.


Thanks joteke, I think I'm slowly getting there.

So the .aspx file inherits the class that you create in the aspx.vb file ?

Then within that class you simply insert all the code that was previously in the <script> section at the top of the .aspx file?

What does the'AutoEventWireup' attribute do?


Also, do i need to import the namespaces in the aspx file or the aspx.vb file ?

duncanhall:

So the .aspx file inherits the class that you create in the aspx.vb file ?

Yes. Basically with partial classes in ASP.NET 2.0 (when using CodeFile attribute) a second partial class is created. This class contains control declarations for controls residing on aspx (removes the need for you to declare them on aspx.vb as you had to do with ASP.NEt v1.x).

These two partial classes are merged when compilation occurs and are used as base class for class which is created when the actual aspx is parsed and compiled (yes, basically the aspx itself)

duncanhall:



Then within that class you simply insert all the code that was previously in the <script> section at the top of the .aspx file?

Yes, note that the code-behind class is now equally part of your page.

duncanhall:


What does the'AutoEventWireup' attribute do?

It indicates whether Page's event are autowired. If enabled, it means that you have a method which matches Page_[EventName] or [ControlID]_[EventName] name so that method would automatically be assigned as event handler for the correspondng event of P?ager or the Control. Normally these event methods need to be wired explicitly.

See:ASP.NET Web Server Control Event Model


If code is in code-behind, then you import in code-behind.
Thanks a lot joteke, everything seems to make sense now, and my first app is pretty much finished!

0 comments:

Post a Comment