Temporary file contains reference to constructor with no arguments when it shouldn't
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 开发者_如何学JAVAconstructor.
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.
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.
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.
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.
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.
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.
精彩评论