views:

69

answers:

3

One of our contractors gave us an ASP.NET 2.0 web site and I'm having a little trouble integrating it into our process. He gave us the project as a zipped directory, no solution file or anything. I can open it in Visual Studio using the Open -> Website option, and the website runs. However, I wanted to set it up as a web application project so that I could add project references to our shared libraries instead of them being linked into the 'bin' directory, and just because I'd prefer to have a regular project really.

I'm running into a weird issue. Let me extract a little code:

HeaderControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="HeaderControl.ascx.cs" Inherits="Controls_WebUserControl" %>

HeaderControl.ascx.cs

public partial class Controls_WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)

Ok, this is a little strange but it seems to be legal at least. The base class has a partial definition in the code-behind (code file?). But then many other controls are defined in the same way, eg

SomeControl.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SomeControl.ascx.cs" Inherits="Controls_WebUserControl" %>

SomeControl.ascx.cs

public partial class Controls_WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)

Expectedly, when I try to build my new web application project that I've transferred the files into I get a bunch of errors about the same method being defined in serveral places. I don't understand why it's not set up with a code behind defining a partial definition of the control class and overriding methods of the base class, but that's sort of beside the point.

What really gets me is this: How does the original work? It runs and it works, so the compiler must be doing something that I don't understand. What happens to the Controls_WebUserControl class?

+1  A: 

Actually you need to replace CodeFile="SomeControl.ascx.cs" with CodeBehind="SomeControl.ascx.cs"

CodeBehind vs CodeFile

Darin Dimitrov
+2  A: 

WHen you change from a web site to a web app, you need to right-click on the project and choose "Convert to Web Application". This should update all the pages.

This is an old link, but I think it's still relevant:

http://webproject.scottgu.com/CSharp/migration2/migration2.aspx

Sam
+1  A: 

I would say that while the provided site works, it is incorrect.

It works because it is a Web Site project (as opposed to a Web Application Project). Web Site projects do not have a namespace for the web pages; instead it compiles each page separately. In other words, you could say that the class name for the pages is ignored.

It fails when you bring it into a Web Application project because that tries to build a namespace and that fails because of the common class names across the web pages and controls.

I'd right click, select Convert to Web Application, and then fix the class names on the pages & controls. That should get you to where you want to be.

Jeff Siver
Thanks! I just read about the asp.net in-memory compilation and your post confirmed my thoughts. And I agree with you - even though this works, this sort of a set up is poor practice. I'm actually going to fix the class names before running the wizard - less files to fix.
Egor