-
Aug 29th, 2007, 08:32 AM
#1
Thread Starter
Addicted Member
Aspx Page code Inline VS Seperate file
Could someone give me their opinion on placing the code in a seperate file as opposed to placing the code in the same file. Maybe give some advantages and disadvantages.
I normally place my code in a seperatefile, but I've been thinking more and more of using the same file. I'm using vStudio 2005
thanks
GEM
-
Aug 29th, 2007, 08:39 AM
#2
Lively Member
Re: Aspx Page code Inline VS Seperate file
I use seperate code files so far I have found that it makes finding code
easier and easier to read but seems that you still end up using code
blocks in the .aspx like <%= somevar %> and having to declaire vars in the .vb as public shared to get the value passed to the aspx file still new
to asp.net so that maybe wrong lol !!
But I do prefer so far having the seprate files.
-
Aug 29th, 2007, 09:23 AM
#3
Thread Starter
Addicted Member
Re: Aspx Page code Inline VS Seperate file
thanks Polecat,
I have always used seperate files too, but I sometimes like to make copies of of aspx pages when I'm about to make a major change. I'm starting to run in some problems and I think I may have some errors in my inherits attributes, code file attribute and my partial class names. If you place your code in the same file, you don't have these attributes to worry about, plus you don't have the partial class to worry about. Also if you want to do a 'Save As' on your page the only thing that gets saved is the .ASPX. , the ASPX.VB doesn't get saved with the new name. Also if you want to import an ASPX page from another web project, you need to make sure you import 2 files not just one. Also, a lot of samples you might get are usually one file only.
After thinking about it the only disadvantage of having the code in the same file is that its not as easy to read. I think I may have answered my own question. I guess my many question is if there are any performance issues.
thanks
-
Aug 30th, 2007, 05:49 AM
#4
Re: Aspx Page code Inline VS Seperate file
Other than developer ease and the fact that it's a lazy man's way of doing things, there is no advantage to this. The inline ASP.NET code is executed after the codebehind's code, therefore making the page even more inefficient than it already may be.
There is no valid reason for using inline ASP.NET, and I'd even say that if you like using inline ASP.NET, might as well go back to classic ASP.
I do not, however, know why this feature was allowed to remain in ASP.NET, possibly a legacy leftover to make transition easier for classic ASP developers. But now that we are allowed to separate UI logic from UI, that's what we should do.
-
Aug 30th, 2007, 03:44 PM
#5
Lively Member
Re: Aspx Page code Inline VS Seperate file
Hi mendhak
I was a bit puzzled by your post above...
I can see the sense in the seperate code behind files that why
when I came over from classic asp I started to use them and found this
the easiest way to track code / design...
As for script blocks how can we avoid them ?
(forgive this im still learning!)
In my Default.aspx.vb I have to do
Code:
Public Shared myString As String
This means I can give it a value from a Sub / function or what ever
Then in the Default.aspx source to use this value I would need to use
inline like
Code:
<asp:Image runat="server" ImageUrl="<%= myString %>" />
So how can inline like this be avoided any pointers ?
Thanks
-
Aug 30th, 2007, 03:52 PM
#6
Re: Aspx Page code Inline VS Seperate file
Firstly, a static/shared variable is available across all requests. Change it in one request, change it in all, which I don't believe you want, seeing as you are applying it at runtime.
You need only mark it as protected ("Friend" in VB, I think?) for it to be accessible in the ASPX.
To answer your question, give the image an ID, then use the following in the code behind. Feel free to move the "myString" declaration to the smallest scope available, preferably method.
Code:
<asp:Image ID="myImageID" runat="Server" />
Sub FooBar()
Dim myString As String = "~/image.png"
myImageID.ImageUrl = myString
End Sub
Last edited by axion_sa; Aug 30th, 2007 at 03:54 PM.
Reason: Clarified sample code.
-
Aug 30th, 2007, 04:00 PM
#7
Re: Aspx Page code Inline VS Seperate file
Originally Posted by polecat
Hi mendhak
I was a bit puzzled by your post above...
I can see the sense in the seperate code behind files that why
when I came over from classic asp I started to use them and found this
the easiest way to track code / design...
As for script blocks how can we avoid them ?
(forgive this im still learning!)
In my Default.aspx.vb I have to do
Code:
Public Shared myString As String
This means I can give it a value from a Sub / function or what ever
Then in the Default.aspx source to use this value I would need to use
inline like
Code:
<asp:Image runat="server" ImageUrl="<%= myString %>" />
So how can inline like this be avoided any pointers ?
Thanks
See, that's just a bad way of doing things (no offense). If the page is dynamically generated and the image is in a data control, simply bind it to whatever field produces the image URL, rather than declaring in code-behind. If you absolutely MUST set it from the codebehind, then you have the wrong idea. Use a mentality similar to this one:
Code:
<asp:Image runat="server" id="img1" />
And in your Page_Load event:
Code:
img1.ImageUrl = "~/images/blah.jpg"
Cleaner, easier to maintain in the future. Your way may very well work, but it'll be a pain in the rear later on when things grow more complicated.
-
Aug 30th, 2007, 04:37 PM
#8
Lively Member
Re: Aspx Page code Inline VS Seperate file
No offence taken timeshifter this is the sort of advice I was after!
Have just changed a few of my code block and made them like you
said up above works a treat and the page actually loads faster!
Still finding my feet with asp.net ! clasic asp let you get away with alot and
was very sloppy asp.net and advice like that helps to make better apps
thank you so much
-
Aug 30th, 2007, 05:43 PM
#9
Thread Starter
Addicted Member
Re: Aspx Page code Inline VS Seperate file
If you have created aspx using in-line code, is it possible to copy the code to another file?
Copy code from inline aspx to aspx.vb ie Default.aspx.vb...does the vb file name have to the the same name as the .aspx ie Default.aspx.vb
- add codefile attribute and set to "CodeFile="Default.aspx.vb"" or the name of your code file?
- add the inherits attribute to Inherits="_Default". I never have really understood this. Does the inhertis attribute have to be the same name as the page name with an underscore? Is the inherits attribute necessary?
- create a class in your codebehind file and call it "Partial Class _Default" all the code from your inline aspx should go between "Partial Class _Default" and the end class.??
You should be ok, as long as your partial class and your inherits attribute match ....and your code file attribute matches the actual file you are using?
Delete all the code from your inline .aspx that you copied to your .aspx.vb?
These are all questions in case they didn't look like questions?
Thanks
GEM
-
Aug 31st, 2007, 02:39 AM
#10
Lively Member
Re: Aspx Page code Inline VS Seperate file
This is the top of the Default.aspx
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
And this is the Default.aspx.vb
Code:
Partial Class _Default
Inherits System.Web.UI.Page
End Class
you can create the Default.aspx.vb and move the code over..
Make a backup of the site first in case the compiler dont like something
or you miss some code in the aspx file. The inherits="" has to be the name of the partial class. which should be the name of the aspx file without the .aspx exept
the default.aspx.vb seems to use _DEfault?
-
Aug 31st, 2007, 04:52 AM
#11
Thread Starter
Addicted Member
Re: Aspx Page code Inline VS Seperate file
thanks polecat
I did that with a couple of pages and it worked. I also played around with the inherits and partial class name. It seems you can name the partial class anything you want as long as you change the inhertis attribute to match the partial class name. I definetly need to understand this because it's so easy to completely screw up a page or a whole website....I know because I've done it. It took me forerver to figure out what the problem was.
GEM
-
Aug 31st, 2007, 07:21 AM
#12
I wonder how many charact
Re: Aspx Page code Inline VS Seperate file
I will simply add that the decision by the VS guys to put control declarations in a separate partial class extended with .designer is pretty silly to me. I know they did it to separate what the designer wrote, but I still like the old web application project style of one markup file and one code-behind file.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|