How do I tell VS2005 about a code-behind file?

A

Alan Silver

Hello,

I have been developing ASP.NET with a text editor so far (better way to
learn initially), and have now been trying Visual Web Developer. I want
to create a project for an existing site and add the files to it. I can
add my .aspx and .cs (code-behind) files, but VWD doesn't know which .cs
file si the code-behind for any particular .aspx file.

When you create a new aspx page in VWD, it automatically puts the
code-behind file with the aspx. Can I make it do this for my existing
files?

TIA
 
G

Gaurav Vaish

: add my .aspx and .cs (code-behind) files, but VWD doesn't know which .cs
: file si the code-behind for any particular .aspx file.

Add an attribute "CodeBehind=<file>.cs" to the "@ Page" Directive on the
top.
 
A

Alan Silver

: add my .aspx and .cs (code-behind) files, but VWD doesn't know which .cs
: file si the code-behind for any particular .aspx file.

Add an attribute "CodeBehind=<file>.cs" to the "@ Page" Directive on the
top.

VWD complained at CodeBehind, saying it was no longer supported. I tried
using CodeFile instead as the ones VWD generates use that, and then it
recognised the .cs file as being the code-behind for the .aspx, but
didn't change the display in the solution explorer. They were still
shown as separate items.

Any other ideas? Thanks.
 
J

Juan T. Llibre

re:
it recognised the .cs file as being the code-behind for the .aspx, but didn't change the
display in the solution explorer. They were still shown as separate items.

I don't understand. They will *always* be shown as separate items.

You can edit *.aspx and *.aspx.cs files separately.
Am I missing something ?
 
A

Alan Silver

it recognised the .cs file as being the code-behind for the .aspx,
I don't understand. They will *always* be shown as separate items.

You can edit *.aspx and *.aspx.cs files separately.
Am I missing something ?

Maybe I didn't explain it clearly enough. In the solution explorer, you
see an entry for the .aspx file, with a small + sign to the left of the
icon. If you click the + sign, a node opens below it for the .cs file.

When I add pre-existing files, they just show as two separate top-level
nodes in the solution explorer. I'm trying to find a way of persuading
VWD to put the .cs file as a node below the .aspx file, so the top-level
nodes are (mostly) .aspx files.

Does that explain it any better?
 
J

Juan T. Llibre

re:
Maybe I didn't explain it clearly enough.

I understand better now.

To get the IDE to understand that the files are related,
just select *both* the *.aspx and the *aspx.cs file
at the same time, and add them simultaneously.

The IDE will display them as you want them to display.




Alan Silver said:
I don't understand. They will *always* be shown as separate items.

You can edit *.aspx and *.aspx.cs files separately.
Am I missing something ?

Maybe I didn't explain it clearly enough. In the solution explorer, you see an entry for
the .aspx file, with a small + sign to the left of the icon. If you click the + sign, a
node opens below it for the .cs file.

When I add pre-existing files, they just show as two separate top-level nodes in the
solution explorer. I'm trying to find a way of persuading VWD to put the .cs file as a
node below the .aspx file, so the top-level nodes are (mostly) .aspx files.

Does that explain it any better?
 
A

Alan Silver

Juan,

Thanks for the tip, but it didn't work. I tried adding the two files
simultaneously as they are (ie with the code-behind file specified using
the Src attribute of the page directive) and it didn't work.

I then deleted them from the project, changed the .aspx file to use
CodeFile (using Notepad) and adding them again, but this didn't work
either. I also tried using COdeBehind, but that didn't work.

Any other ideas? Thanks for the reply.
re:
Maybe I didn't explain it clearly enough.

I understand better now.

To get the IDE to understand that the files are related,
just select *both* the *.aspx and the *aspx.cs file
at the same time, and add them simultaneously.

The IDE will display them as you want them to display.
 
J

Juan T. Llibre

re:
Thanks for the tip, but it didn't work. I tried adding the two files

It works fine for me.

Let's review the mechanics and make sure you're doing what you should.

1. Click "Website" on the VS.NET 2005 menu for VWD.
2. Click "Add Existing Item".
3. Navigate to the folder where the files exist.
4. Select your aspx file ( click on it *once* to highlight it ).
5. Hold the Control key and select the .cs file which corresponds to the aspx.
6. Now you have *both* files highlighted
7. Click the "Add" button.

Is that what you're doing ?

When I do that, the aspx file is added to my Solution Explorer,
with the corresponding codebehind .cs file linked to it.

I can see the linked codebehind .cs file by clicking the plus sign (+)
in front of the aspx file.






Alan Silver said:
Juan,

Thanks for the tip, but it didn't work. I tried adding the two files simultaneously as
they are (ie with the code-behind file specified using the Src attribute of the page
directive) and it didn't work.

I then deleted them from the project, changed the .aspx file to use CodeFile (using
Notepad) and adding them again, but this didn't work either. I also tried using
COdeBehind, but that didn't work.

Any other ideas? Thanks for the reply.
 
A

Alan Silver

re:
It works fine for me.

Let's review the mechanics and make sure you're doing what you should.

I did exactly what you said, except that I was adding them to an
existing web site instead of starting a new project, but that shouldn't
be relevant.

What does the page directive look like in your .aspx file? I tried three
variations, but maybe I'm missing something. Mine looks like...

<%@ Page Language="C#" Debug="true" Src="ProductList_cb.cs"
Inherits="ProductList" %>

As I said, I tried changing Src to CodeBehind and CodeFile, but it
didn't help.

Thanks for the help. Any further help would be very gratefully received.
Ta ra
1. Click "Website" on the VS.NET 2005 menu for VWD.
2. Click "Add Existing Item".
3. Navigate to the folder where the files exist.
4. Select your aspx file ( click on it *once* to highlight it ).
5. Hold the Control key and select the .cs file which corresponds to the aspx.
6. Now you have *both* files highlighted
7. Click the "Add" button.

Is that what you're doing ?

When I do that, the aspx file is added to my Solution Explorer,
with the corresponding codebehind .cs file linked to it.

I can see the linked codebehind .cs file by clicking the plus sign (+)
in front of the aspx file.
 
J

Jevon

What's the name of your page? If, for example, it's ProductList.aspx, your
code-behind file should be called ProductList.aspx.cs I think.

Jevon


Alan Silver said:
re:

It works fine for me.

Let's review the mechanics and make sure you're doing what you should.

I did exactly what you said, except that I was adding them to an existing
web site instead of starting a new project, but that shouldn't be
relevant.

What does the page directive look like in your .aspx file? I tried three
variations, but maybe I'm missing something. Mine looks like...

<%@ Page Language="C#" Debug="true" Src="ProductList_cb.cs"
Inherits="ProductList" %>

As I said, I tried changing Src to CodeBehind and CodeFile, but it didn't
help.

Thanks for the help. Any further help would be very gratefully received.
Ta ra
 
D

Damien

Alan said:
I did exactly what you said, except that I was adding them to an
existing web site instead of starting a new project, but that shouldn't
be relevant.

What does the page directive look like in your .aspx file? I tried three
variations, but maybe I'm missing something. Mine looks like...

<%@ Page Language="C#" Debug="true" Src="ProductList_cb.cs"
Inherits="ProductList" %>

As I said, I tried changing Src to CodeBehind and CodeFile, but it
didn't help.

Thanks for the help. Any further help would be very gratefully received.
Ta ra
In Visual Studio 2003 you can sometimes get this problem. May or may
not work the same in 2005, or there may be something similar, but after
adding all the files to the project, close down Visual Studio, then
open the project file in Notepad.

It'll have a whole load of:
<File
RelPath = "<Name of CS File>"
SubType = "Code"
BuildAction = "Compile"
/>

You want to change these to resemble:
<File
RelPath = "<Name of CS File>"
DependentUpon = "<Name of Aspx file>"
SubType = "ASPXCodeBehind"
BuildAction = "Compile"
/>

Easiest way to get format correct is to add a new page as you
described, then open the project file in notepad and find how the two
files have been added.

Damien
 
A

Alan Silver

What's the name of your page? If, for example, it's ProductList.aspx, your
code-behind file should be called ProductList.aspx.cs I think.

Ooh, I wonder if that's the reason then? I adopted the convention of
ferret.aspx and ferret.cs for the two pages. I didn't like the .aspx.cs
idea (don't know why), and given that it seemed to be simply one way of
doing it, I picked my own.

<pause for experiment>

Well cut off both my legs and call me Shorty!! It worked. OK, so now I
know how to do it, I'll have to go and change the name of all my pages.
What a pain. I wonder why they insisted on that naming convention. It
seems very restrictive. Why can't I name my files anything I like?

Thanks for the reply.
 
A

Alan Silver

In Visual Studio 2003 you can sometimes get this problem. May or may
not work the same in 2005, or there may be something similar, but after
adding all the files to the project, close down Visual Studio, then open
the project file in Notepad.

It'll have a whole load of:
<File
RelPath = "<Name of CS File>"
SubType = "Code"
BuildAction = "Compile"
/>

You want to change these to resemble:
<File
RelPath = "<Name of CS File>"
DependentUpon = "<Name of Aspx file>"
SubType = "ASPXCodeBehind"
BuildAction = "Compile"
/>

Easiest way to get format correct is to add a new page as you described,
then open the project file in notepad and find how the two files have
been added.

Damien,

Thanks for the reply, but I can't see this info anywhere. There is a
..sln file, but that doesn't contain anything like you suggested. The
only other project file I can see is a .suo file, which is binary.

Which file should I open? For interest, the .sln file contains...


Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Web Developer Express 2005
Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "C:\...\FirstTest\", "..\..\..\WebSites\FirstTest\", "{765AD130-A4CC-
4A92-BBD7-674C4E832F2E}"
ProjectSection(WebsiteProperties) = preProject
Debug.AspNetCompiler.VirtualPath = "/FirstTest"
Debug.AspNetCompiler.PhysicalPath = "..\..\..\WebSites\FirstTest\"
Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\FirstTest\"
Debug.AspNetCompiler.Updateable = "true"
Debug.AspNetCompiler.ForceOverwrite = "true"
Debug.AspNetCompiler.FixedNames = "true"
Debug.AspNetCompiler.Debug = "True"
Release.AspNetCompiler.VirtualPath = "/FirstTest"
Release.AspNetCompiler.PhysicalPath = "..\..\..\WebSites\FirstTest\"
Release.AspNetCompiler.TargetPath = "PrecompiledWeb\FirstTest\"
Release.AspNetCompiler.Updateable = "true"
Release.AspNetCompiler.ForceOverwrite = "true"
Release.AspNetCompiler.FixedNames = "true"
Release.AspNetCompiler.Debug = "False"
VWDPort = "1031"
DefaultWebSiteLanguage = "Visual C#"
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|.NET = Debug|.NET
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{765AD130-A4CC-4A92-BBD7-674C4E832F2E}.Debug|.NET.ActiveCfg = Debug|.NET
{765AD130-A4CC-4A92-BBD7-674C4E832F2E}.Debug|.NET.Build.0 = Debug|.NET
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
 
J

Juan T. Llibre

re:
Ooh, I wonder if that's the reason then? I adopted the convention of ferret.aspx and
ferret.cs for the two pages. I didn't like the .aspx.cs idea (don't know why), and given
that it seemed to be simply one way of doing it, I picked my own.

The road to hell is paved with good conventions, err, intentions... ;-)





Alan Silver said:
What's the name of your page? If, for example, it's ProductList.aspx, your
code-behind file should be called ProductList.aspx.cs I think.

Ooh, I wonder if that's the reason then? I adopted the convention of ferret.aspx and
ferret.cs for the two pages. I didn't like the .aspx.cs idea (don't know why), and given
that it seemed to be simply one way of doing it, I picked my own.

<pause for experiment>

Well cut off both my legs and call me Shorty!! It worked. OK, so now I know how to do
it, I'll have to go and change the name of all my pages. What a pain. I wonder why they
insisted on that naming convention. It seems very restrictive. Why can't I name my files
anything I like?

Thanks for the reply.
 
J

Jevon

Glad you got it to work.

The naming convention does make sense from a graphical viewpoint - you might
have a page called ferret.aspx, with the code behind called ferret.aspx.cs -
it's the code for the ferret.aspx page, so it follows it would be "below" it
in a tree structure [constructed from multiple extensions]. You might also
have a ferret class, which would make sense to save as ferret.cs. With your
naming, your ferret class would have to be saved as something like
ferret.class.cs.
Admittedly, you might be the type of coder that likes to combine all objects
into one, for example, objects.cs file, but many coders, myself included,
tend to keep to one class per file, treating them as separate units.

As a side note, one could argue that given the aspx naming convention, the
Winforms naming is wrong, or vice-versa (frmMain.cs, frmMain.Designer.cs and
frmMain.resx, rather than frmMain.frm, frmMain.frm.cs and frmMain.frm.resx)
but they're developed by different teams and haven't been similarised -
can't think of the correct term :)

Hope that makes sense... :)

Jevon


Alan Silver said:
What's the name of your page? If, for example, it's ProductList.aspx,
your
code-behind file should be called ProductList.aspx.cs I think.

Ooh, I wonder if that's the reason then? I adopted the convention of
ferret.aspx and ferret.cs for the two pages. I didn't like the .aspx.cs
idea (don't know why), and given that it seemed to be simply one way of
doing it, I picked my own.

<pause for experiment>

Well cut off both my legs and call me Shorty!! It worked. OK, so now I
know how to do it, I'll have to go and change the name of all my pages.
What a pain. I wonder why they insisted on that naming convention. It
seems very restrictive. Why can't I name my files anything I like?

Thanks for the reply.
 
A

Alan Silver

Jevon,

Yup, it makes lots of sense. I guess this is one of those irritating
things that you get when learning something new, especially when you
start off doing it by hand (all text editor so far, this is my first
dabble with VWD).

Still, all part of the learning experience.

Ta ra
Glad you got it to work.

The naming convention does make sense from a graphical viewpoint - you might
have a page called ferret.aspx, with the code behind called ferret.aspx.cs -
it's the code for the ferret.aspx page, so it follows it would be "below" it
in a tree structure [constructed from multiple extensions]. You might also
have a ferret class, which would make sense to save as ferret.cs. With your
naming, your ferret class would have to be saved as something like
ferret.class.cs.
Admittedly, you might be the type of coder that likes to combine all objects
into one, for example, objects.cs file, but many coders, myself included,
tend to keep to one class per file, treating them as separate units.

As a side note, one could argue that given the aspx naming convention, the
Winforms naming is wrong, or vice-versa (frmMain.cs, frmMain.Designer.cs and
frmMain.resx, rather than frmMain.frm, frmMain.frm.cs and frmMain.frm.resx)
but they're developed by different teams and haven't been similarised -
can't think of the correct term :)

Hope that makes sense... :)

Jevon
 
A

Alan Silver

What's the name of your page? If, for example, it's ProductList.aspx,
your code-behind file should be called ProductList.aspx.cs I think.

OK, I've been contemplating this, and I have another question.

I have a page on which products are displayed. In order to give the web
site owner some flexibility, the page checks a config file and loads the
user control specified. That user control actually does the work of
pulling the product details from the database and setting the visual
controls to the right values. There can be any number of user controls,
although in reality, they are all .ascx files which use one common .cs
code-behind file.

At the moment they are named like this...

ProductStyle1.ascx
ProductStyle2.ascx
ProductStyle3.ascx
....etc...

and the .cs file is named Product.cs. At the moment, I can add and
delete the .ascx files as I wish, and the admin page for the site owner
just looks to see which .ascx files exist and offers them as the choice.

Now, how would I do this in VWD? I really would like it to know (and
show visually) that all of these .ascx files use Product.cs as the
code-behind, but I can't do this by renaming it (say to
ProductStyle1.ascx.cs), as that would a) only allow it to be tied to one
of the .ascx files and b) specifically tie it to that one, meaning that
I couldn't remove the .ascx file later.

Any suggestions? TIA
 
J

Jevon

Without seeing the code (i.e. this might not be suitable) I'd suggest having
a look at Themes to see if they can do anything for you. Can you post
examples of the .ascx and/or explain how they differ?

Jevon
 
A

Alan Silver

Without seeing the code (i.e. this might not be suitable) I'd suggest having
a look at Themes to see if they can do anything for you. Can you post
examples of the .ascx and/or explain how they differ?

At the simplest, you could have two .ascx files that contains exactly
the same HTML and server tags, but laid out slightly differently, eg:-

ProductStyle1.ascx
==============
<%@ Inherits="ShowProduct" CodeBehind="ShowProduct.cs" %>
<asp:Literal ID="litProductName" Text="" RunAt="server" />
<br><img ID="imgPic" src="" border="0" alt="" Runat="Server" />

and ...

ProductStyle2.ascx
==============
<%@ Inherits="ShowProduct" CodeBehind="ShowProduct.cs" %>
<img ID="imgPic" src="" border="0" alt="" Runat="Server" />
<br><asp:Literal ID="litProductName" Text="" RunAt="server" />

Obviously, that's a very much simplified example, but the principle is
there. The .ascx files create different layouts by having the HTML and
server tags in different places. The code-behind file doesn't care where
the server controls are, it just populates them.

The result is that different products on the site can be displayed in
different ways, and the site owner can choose which style to use for any
given product.

Hopefully you can now see why I have multiple .ascx files with just one
code-behind file. I don't think themes would do this as they are more
global, or at least page-specific than my method. I have just one
Products.aspx page that takes a querystring parameter and loads the
appropriate info. Part of that is choosing the right .ascx file.

Does that make it any clearer? Thanks for the reply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top