[SOLVED] Create partial classes in Asp.Net

Issue

I have a small web application.

This is what I want to do: Create partial classes to split up a code behind file.
Why am I not able to create partial classes directly?

If this is the codebehind file:

using System;
using System.Configuration....


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Test();
    }
}

And also create this class:

public partial class _Default
{
    public void Test()
    {
    }
}

I get an error message, Test doesn’t exists in current context.

Why is this?

I get everything to work if I create a couple of partial classes which inherits from System.Web.UI.Page and then inherit my codebehind file from this class (the result of the partial classes). Is this the only way?

Has is something to do with that the codebehind and the aspx-page is dynamically compiled into a class?

Would appreciate if anyone could answer my problem. Things are working out right now but I would prefer to create the partial classes at the same “level” as the codebehind and avoid to use inheritance.

Thank you /John

(note that I have underscore in front of both classes but only one is visible in the preview, I don’t know why..)

Solution

1) Put your partial classes in the app_code directory (I’ve never tried this)
2) Convert your application into a Web Application Project from a Website Project

A WAP is more like a dll or winforms project, which is compiled before runtime (although the ui can be updatable) and puts classes in namespaces. Website projects are compiled using the aspnet_compiler which operates differently (e.g., dynamically named assemblies) than the standard compiler.

Answered By – user1228

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *