views:

118

answers:

5

I have some WebUserControls that take a parameter to determine how they initialize themselves. However, when I compile the page I get the error

'ControlName' does not contain a constructor that takes '0' arguments

I am not calling it anywhere without an argument, it appears that a reference is being generated in a temporary file. I have another control that doesn't have a empty constructor and it is not requiring one to be created there so it shouldn't have to have one.

The actual error looks like this:

Compiler Error Message: CS1729:

'ControlName' does not contain a constructor that takes '0' arguments

Source Error:

Line 108: private static bool @__initialized; Line 109: Line 110:
[System.Diagnostics.DebuggerNonUserCodeAttribute()] Line 111: public control_ascx() { Line 112:
((global::ControlNamespace)(this)).AppRelativeVirtualPath = "~/ControlName.ascx";

Source File: (Path)Local\Temp\Temporary ASP.NET Files\root\1aca8e08\3fab105e\App_Web_controlname.ascx.cdcab7d2.tzm0xzkd.0.cs Line: 110

The control looks like this:

public partial class ControlName: System.Web.UI.UserControl
{
    public ControlName(IParameter parameter)
    {
        Method(parameter);
    }
}

EDIT: Based on the comments below it would appear that I have a reference to the control in a designer file...somewhere... All references that I can find are located in code behinds with one exception. The declaration in the .ascx file:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ControlName.ascx.cs" Inherits="Control" %>

I thought that this was the culprit but upon further inspection I have other controls which have a similar declaration and do NOT require a default constructor.

How can I find the location where the control is referenced from?

Solution:

I had reference tags

<%@ Reference Control="~/ControlName.ascx" %>

leftover from earlier that apparently count as a designer element.

The answer below does not actually contain this information but is a good description of what the problem was.

A: 

The CLR is trying to call the default constructor of the super class to your class, but it cannot. Put an explicit call to a suitable base constructor.

Segfault
A: 

when you have ctor without parameters it tries to call parent's class ctor without parameters. but it's base class doesn't have one.

Andrey
A: 

I do not know if this is the case for you. I have in the past gotten that compile error when working with object serialization. If I remember correctly, when we were using XML serialization exclusively, we would get this error. When deserializing the object, the runtime was using the default constructor, so if it did not have one, it was flagged by the compiler.

Rob Goodwin
+2  A: 

I'm no expert on web development, but it looks like you've got an instance of the control placed on a web form designer, and that designer is generating code that expects a parameterless constructor.

If this is the case, then to solve this problem, you should either remove the control from the form or add a parameterless constructor.

Jeffrey L Whitledge
Are you saying that the page where the control is used may be referencing the control in it's designer portion and expecting a constructor with no parameters?
mwright
Yes, that's what it sounds like. I have run into this problem before when creating XAML forms. The designer has to call some kind of constructor to generate the object.
Jeffrey L Whitledge
@mwrite - @Jeffrey is correct. Both the ASP.NET and the WinForms designer depends on a UI element having a parameterless constructor to work. If you only ever create the control dynamically and add it to an existing Form or Page, you'll be fine. But as soon as you attempt to drag and drop it on a form at design time, the .designer.cs file will have a call to the default constructor in the InitializeComponents method.
Nick
@Nick, right, the confusing part is I'm only creating the control dynamically. I never used it in a drag and drop fashion. Is there a good way to find where it thinks this is happening?
mwright
@mwright - If ASP works anything like XAML does, then the files referenced in the error message might help. They can usually be found in the "obj" directory in your project, and they should contain the actual C# that is being generated as well as comments referencing the original file.
Jeffrey L Whitledge
A: 

Based on your edit, I think this may actually be because of the name you chose... Control. There are lots of Control classes, and it could be that the declare in the ascx is having issues with a namespace conflict. Try renaming your Control to something else.

Nick
Control isn't the actual name of the control, sorry if that was misleading, it was merely a placeholder. I'll modify it to reflect that.
mwright