开发者

Type '_Default' already defines a member called 'Page_Load' with the same parameter types

I've been renaming some classes and packages in my aspx project and now i have this error:

"Type '_Default' already defines a member called 'Page_Load' with the same parameter types"

I have two aspx pages. In the default.aspx codebehind i see:

Default.aspx:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="_Default" %>

Default.aspx.cs:

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        //error line under 'Page_Load'
    }

search.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="search.aspx.cs" Inherits="_Defaul开发者_如何转开发t" %>

search.aspx.cs:

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

    }

Every new ASPX page I add to my project is automaticly added to some namespace.

I've tried changing the inherits attribuut. But i couldn't find a way to fix this error and to get rid of the namespace.

I'm using Visual Studio 2010.

Type '_Default' already defines a member called 'Page_Load' with the same parameter types


Every page you add is automatically configured to namespace depending on your folder structure. I don't see enough code and structure, but are you sure, that you don't have the Page_Load defined twice? At least the error message says so. Does it behave same even when you use different class name than _Default ?

After edits:

Yea, there we go. You define same class (_Default) in both Default.aspx and Search.aspx ... You should rename your classes according to conventions. ie: use class "Default" in your Default.aspx and use class "Search" in your Search.aspx


Double click the error, temporarily rename the Page_Load to something else. Go down into the body of the function and type Page_Load. Press F12. That will get you to the place where you have second Page_Load method already defined. You'll probably see that it's in another partial _Default class in the same namespace.


Just to add up a specific case.

You can come across to this situation when you convert Web Site into Web Application.

When your project in form of Web Site, when you add for example Default.aspx into two different folders they both created without namespace with the same class name. Both declared partial and it is just fine. But when you convert into Web Application and try to build they start conflicting as they are in the same namespace, declared partial and have their own Page_Load methods.

One of the solutions can be giving distinct class names or encapsulating into different namespaces in accordance with the folder structure.


Since your class is public partial class _Default it's probably some naming that is causing the problem. Try to identify the other part(s) of _Default. Since it's a partial class you're able to have as many partials as you want.. Problem is probably that Page_Load is defined in one of those.


Below follows issues I have encountered when copying files into my solution, clicking on the reported error or on "Go to Definition" mislead me to spot the cause. The hint is one line above..... !

I'm exposing the Problem AND how I finally Resolved it.

Errors when building the application:

  • Error 1 Type 'Solution1.Web.yourABC' already defines a member called 'Page_Load' with the same parameter types
    C:\\trunk\Solution1.Web\yourABC.aspx.cs 12 24
    Solution1.Web

  • Error 2 Type 'Solution1.Web.yourABC' already defines a member called 'Page_Load' with the same parameter types
    C:\\trunk\Solution1.Web\GuideABT.aspx.cs 12 24
    Solution1.Web

How the problem arose: I copy/pasted a file .aspx in the same solution to make a new file. C#: Error like below started to appear; worst other misleading errors started to impact the application at runtime:

* Be aware that error 1 IS NOT an error it is CORRECT, as it is the source code

Error    1    Type 'Solution1.Web.yourABC' already defines a member called 'Page_Load' with the same parameter types    C:\<folderpath>\trunk\Solution1.Web\yourABC.aspx.cs    12    24    Solution1.Web 

Error    2    Type 'Solution1.Web.yourABC' already defines a member called 'Page_Load' with the same parameter types    C:\<folderpath>\trunk\Solution1.Web\GuideABT.aspx.cs    12    24    Solution1.Web 

Both classes "Page_Load" are empty, normally they are generated automatically by the Visual Studio Engine

Solution: Change the .cs file of the newly create/pasted aspx page to reflect the page name after the Class "name". In this case "GuideABT.aspx" is the new pasted & renamed aspx file:

Correction on Error 1: NO CORRECTION NEEDED as it is the copied from file. MAKE SURE THAT the name of the file and the name of the class reference ARE the same in the .cs files:

File name yourABC.aspx, check the .cs extension files:

namespace Solution1.Web
{
    public partial class yourABC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Correction on Error 2: MODIFY the pasted file. Correct the CLASS NAME to reflect the name of the .aspx file.

File name GuideABT.aspx, check the .cs extension files: ORIGINAL code in .cs

namespace Solution1.Web
{
    public partial class *yourABC* : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

CORRECTED this code in .cs TO

namespace Solution1.Web
{
    public partial class **GuideABT** : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}

Issue RESOLVED.

Cheers.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜