Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

codebehind html / code seperation

Status
Not open for further replies.

nooro

Programmer
Feb 7, 2003
22
0
0
GB
I have a quick question. When I've seperated my aspx and aspx.cs files in an asp.net application till now, I've used the Src attribute of the page directive to point the aspx to the application code held within the .cs file. (to be compiled dynamically server side).

This has worked fine for me, using web matrix for my development. Now however, in the development of a much larger application (a team exercise!). I have a need to create it as a vs.net project, which if I understand corrently requires me (in order to take advantage of vs.net) to use the CodeBehind attribute within the page directive to create a client side compiled assembly.

Previously, it was a simple matter to have aspx files residing in seperate directories to the cs files, a requirement for the site. I can't figure out how to make vs.net see, and happily deal with this setup. It seems to want to see a WebForm as a single object of three parts (WebForm1.aspx, WebForm1.aspx.cs, WebForm1.resx), which all must reside in the same directory.

Can anyone tell me how I may get around this?
 
further to this, the directive :

<%@ Page language=&quot;c#&quot; Codebehind=&quot;WebForm1.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;WebApplication5.WebForm1&quot; %>

works from within WebForm1.aspx and WebForm2.aspx if WebForm1.aspx.cs is in the same directory, but if I move it away, I can't point the CodeBehind to the cs file :-( I would have thought that one of the following would get me closer to where I want to be???

CodeBehind=&quot;code/WebForm1.aspx.cs&quot;
CodeBehind=&quot;/code/WebForm1.aspx.cs&quot;
CodeBehind=&quot;code\WebForm1.aspx.cs&quot;
CodeBehind=&quot;\code\WebForm1.aspx.cs&quot;
 
From the .NET Framework SDK Documentation - @ Page directive:

Src
Specifies the source file name of the code-behind class to dynamically compile when the page is requested. You can choose to include programming logic for your page either in a code-behind class or in a code declaration block in the .aspx file.
Note RAD designers, such as Visual Studio .NET, do not use this attribute. Instead, they precompile code-behind classes and then use the Inherits attribute.

So, if I understand this correctly, VS precompiles the codebehind class, and then uses the Inherits property, so the CS file doesn't have to be around anymore, only the DLL has to be in the webapplications /bin directory.

regards,
Blaxo
 
As far as I understand it, yes. You only need to actually deploy onto your production server, the aspx and dll files. But in order to arrive at the point where I have a working dll, I need vs.net to be able to see aspx / aspx.cs files in seperate directories as a single entity. Basically, to be able to specify a path within the CodeBehind attribute, which the ide will see, understand, and be able to work with.
 
By default visual studio places the code behind file in the same directory as the aspx page file. While I have never tried it, theoretically this should work:

CodeBehind=&quot;code\WebForm1.aspx.cs&quot;

meaning that in the directory containing your page files (.aspx) there is a folder called &quot;code&quot; where you have moved your codebehid files (.cs).

The question I have is &quot;why would you want to do this?&quot; Visual Studio is so tricky whenever you try to do something outside of the default bahavior. I have found that, with this IDE, it is best for me to just adapt to it's way of doing things.

Maybe we can find a way to work around your problem...

HTH


David
[pipe]
 
It's basically to facilitate language enabling the site. I wanted to have a directory of english aspx pages, a directory of german aspx pages etc, with common code driving each language site.

This was my own way of doing it, and it works a treat if you use src=&quot;../code/index.aspx.cs&quot; and forgoe the use of vs.net. I'm looking into other ways of language enabling the application, I know there are proper ways to go about this task (I've biiig wad of documentation to read through I found earlier after posting this thread!), so I'm hopeful of finding the answer elsewhere.

And no, annoyingly, vs.net wont let you use CodeBehind=&quot;code\WebForm1.aspx.cs&quot; as far as I can see. And believe me I've tried :(
 
First of all, as you indicated, there other ways of localizing the site and I belive .NET has some built in functionality for this. However, it may in fact be easier or more feasible for you to just create a separate html page for each language version (as you did in the past). If you decide to go that route, here's what you do:

Create a base-class derived from System.Web.UI.Page containing the common functionality for versions. In your codebehind class for each page, inherit from your new base class instead of System.Web.UI.Page. Don't do anythign else in the code behind classes for your pages - they will all get their functionality from the common base class.

If that does not make sense, let us know and perhaps someone will provide a more detailed description.
HTH

David
[pipe]
 
The way I understand it, the CodeBehind element is a dummy tag included for reference purposes only.

Inherit simply looks to adopt the functionality of a Page class within an assembly. In other words, if your aspx.cs file is already compiled, there will be an assembly .dll. To reference it, simply go to Project->Add Reference->Projects tab then browse to the necessary .dll. Once adding the reference, you will be able to use the namespaces of the assembly.

Src should still work fine for VS.NET (instead of Inherits which looks for the pre-compiled class). You'd just have to tweak the path, I think.
 
nooro,

I found this article on localization that may be of help to you:

The CodeBehind tag in the Page Directive tells Visual Studio what file contains the codebehind class, so that when you select &quot;view code&quot;, it knows what page to open, and also when you wire-up event handlers in your aspx designer, it knows what file to put the handler code in.

Regards,



David
[pipe]
 
Thanks for all your comments, I'll certainly take a look at that link dragonwell. I've a fair bit of reading ahead of me today! After which I will return and decide exactly how I'm going to proceed ^_^
 
OK - if anyone's interested, I believe it to be practically impossible to do what I needed with vs.net. It just doesn't want you to work like that. Current thinking for our project is to relent and use the Globalization (sic :p) namespace.

Only problem I can see is that, because of the nature of the site we're building, it would be too unwieldy to hold content in the database (a lot of the articles I've been reading have suggested this is the way to do it - funnily enough for article based type of sites, because it suits that model). So *all* textual content on the site will be held in .resx resource files (or the satellite equivalent).

My only worry is that holding such large swathes of data in these files will have a performance hit on the application. Anyone got any opinions on this subject??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top